Mars_RadioGroup//CheckBox





    


    

        

        
    


    

    

    



--------------------------------------------------------

package com.example.pengtuanyuan.demo0112;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private RadioGroup radioGroup=null;
    private RadioButton radioButton1=null;
    private RadioButton radioButton2=null;
    private CheckBox swimBox=null;
    private CheckBox runBox=null;
    private CheckBox fishBox=null;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        radioGroup=(RadioGroup)findViewById(R.id.genderGroup);
        radioButton1=(RadioButton)findViewById(R.id.radioButton1);
        radioButton2=(RadioButton)findViewById(R.id.radioButton2);

        swimBox=(CheckBox)findViewById(R.id.checkbox1);
        runBox=(CheckBox)findViewById(R.id.checkbox2);
        fishBox=(CheckBox)findViewById(R.id.checkbox3);


        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                if (radioButton1.getId()==checkedId){
                    System.out.println("checked is female");
                    Toast.makeText(MainActivity.this,"女",Toast.LENGTH_SHORT).show();
                }else if (radioButton2.getId()==checkedId) {
                    System.out.println("checked is male");
                    Toast.makeText(MainActivity.this,"男",Toast.LENGTH_SHORT).show();
                }
            }
        });


        swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if (isChecked){
                    System.out.println("checked is swimming");
                }else {
                    System.out.println("checked is unchecked");
                }

            }
        });

        runBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if (isChecked){
                    System.out.println("checked is running");
                }else {
                    System.out.println("checked is unchecked");
                }

            }
        });

        fishBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if (isChecked){
                    System.out.println("checked is fishing");
                }else {
                    System.out.println("checked is unchecked");
                }

            }
        });


    }
}

Mars_RadioGroup//CheckBox_第1张图片
1.png

你可能感兴趣的:(Mars_RadioGroup//CheckBox)