RadioGroup、RadioButton(单选按钮)、CheckBox(多选按钮)和Toast的使用方法

先上效果图:


核心代码实现:

1、RadioTestActivity.java

public class RadioTestActivity extends Activity
{
    // 对控件对象进行声明
    RadioGroup genderGroup;
    RadioButton maleBtn;
    RadioButton femaleBtn;
    CheckBox swimBox;
    CheckBox runBox;
    CheckBox readBox;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        init();
        // 为RadioGroup设置监听器,点击其中的任何一个按钮都会调用此监听器,这里的监听器和Button控件的监听器有所不同
        // RadioGroup.OnCheckedChangeListener
        genderGroup
                .setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
                {
                    // group是当前被点击的组,checkedId是当前组中被选中RadioButton的id
                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId)
                    {
                        // 传入的id等于男性按钮的id时
                        if (checkedId == maleBtn.getId())
                        {
                            // 第一个参数 当前Activity
                            // 第二个参数 显示文本
                            // 第三个参数 显示时间
                            Toast.makeText(RadioTestActivity.this, "male",
                                    Toast.LENGTH_LONG).show();
                            // 传入的id等于女性按钮的id时
                        } else if (checkedId == femaleBtn.getId())
                        {
                            Toast.makeText(RadioTestActivity.this, "female",
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
                });
        // CompoundButton.OnCheckedChangeListener CheckBox是CompoundButton的子类
        // 为多选按钮添加监听器
        swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked)
            {
                if (isChecked)
                {
                    Toast.makeText(RadioTestActivity.this, "swim is check",
                            Toast.LENGTH_SHORT).show();
                } else
                {
                    Toast.makeText(RadioTestActivity.this, "swim is uncheck",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
        runBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked)
            {
                if (isChecked)
                {
                    Toast.makeText(RadioTestActivity.this, "run is check",
                            Toast.LENGTH_SHORT).show();
                } else
                {
                    Toast.makeText(RadioTestActivity.this, "run is uncheck",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
        readBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked)
            {
                if (isChecked)
                {
                    Toast.makeText(RadioTestActivity.this, "read is check",
                            Toast.LENGTH_SHORT).show();
                } else
                {
                    Toast.makeText(RadioTestActivity.this, "read is uncheck",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    private void init()
    {
        // 通过控件的ID来得到代表控件的对象
        genderGroup = (RadioGroup) findViewById(R.id.genderGroup);
        maleBtn = (RadioButton) findViewById(R.id.maleBtn);
        femaleBtn = (RadioButton) findViewById(R.id.femaleBtn);
        swimBox = (CheckBox) findViewById(R.id.swim);
        runBox = (CheckBox) findViewById(R.id.run);
        readBox = (CheckBox) findViewById(R.id.read);
    }
}

2、main.xml


    
    
    
        
        
    
    
    
    
    

3、strings.xml


    Hello, I am harvey!
    RadioTest
    
    性别
    
    
    
    爱好
    游泳
    跑步
    阅读

你可能感兴趣的:(RadioGroup、RadioButton(单选按钮)、CheckBox(多选按钮)和Toast的使用方法)