您现在的位置: 万盛学电脑网 >> 程序编程 >> 网络编程 >> 安卓开发 >> 正文

Android开发之选项组件

作者:佚名    责任编辑:admin    更新时间:2022-06-22

一、基础知识:

单项选择组件 :  RadioGroup   互相排斥

多项选择组件 :  CheckBox      彼此独立

二、代码展示:

1."Activity_08srcyanactivity_08MainActivity.java"

[java]

package yan.activity_08; 

 

import android.os.Bundle; 

import android.app.Activity; 

import android.view.Menu; 

import android.widget.CheckBox; 

import android.widget.CompoundButton; 

//import android.widget.CompoundButton.OnCheckedChangeListener;  

import android.widget.CompoundButton.OnCheckedChangeListener; 

import android.widget.RadioButton; 

import android.widget.RadioGroup; 

import android.widget.Toast; 

 

public class MainActivity extends Activity { 

    private RadioGroup  sixRadioGroup; 

    private RadioButton femaleRadioButton; 

    private RadioButton maleRadioButton; 

    private CheckBox swimCheckBox; 

    private CheckBox runCheckBox; 

    private CheckBox readCheckBox; 

     

    @Override 

    protected void onCreate(Bundle savedInstanceState) { 

        super.onCreate(savedInstanceState); 

        setContentView(R.layout.main); 

         

        sixRadioGroup = (RadioGroup)findViewById(R.id.myRadioGroup); 

        femaleRadioButton = (RadioButton)findViewById(R.id.myRadioButton1); 

        maleRadioButton = (RadioButton)findViewById(R.id.myRadioButton2); 

        swimCheckBox = (CheckBox)findViewById(R.id.swim); 

        runCheckBox = (CheckBox)findViewById(R.id.run); 

        readCheckBox = (CheckBox)findViewById(R.id.read); 

         

         

        class RadioGroupCheckBoxListener implements RadioGroup.OnCheckedChangeListener{ 

 

            @Override 

            public void onCheckedChanged(RadioGroup group, int checkedId) { 

                // TODO Auto-generated method stub  

                if(checkedId == femaleRadioButton.getId()) 

                { 

                    Toast.makeText(MainActivity.this, R.string.female_y_note, Toast.LENGTH_SHORT).show(); 

                }else if(checkedId == maleRadioButton.getId()) 

                { 

                    Toast.makeText(MainActivity.this, R.string.male_y_note, Toast.LENGTH_SHORT).show(); 

                } 

            } 

             

        } 

        class SwimCheckBoxListener implements OnCheckedChangeListener{ 

 

            @Override 

            public void onCheckedChanged(CompoundButton buttonView, 

                    boolean isChecked) { 

                // TODO Auto-generated method stub  

                if(isChecked) 

                { 

                    Toast.makeText(MainActivity.this, R.string.swim_y_note, Toast.LENGTH_SHORT).show(); 

                }else{ 

                    Toast.makeText(MainActivity.this, R.string.swim_n_note, Toast.LENGTH_SHORT).show(); 

                } 

            } 

        } 

         

        class RunCheckBoxListener implements OnCheckedChangeListener{ 

 

            @Override 

            public void onCheckedChanged(CompoundButton buttonView, 

                    boolean isChecked) { 

                // TODO Auto-generated method stub  

                if(isChecked) 

                { 

                    Toast.makeText(MainActivity.this, R.string.run_y_note, Toast.LENGTH_SHORT).show(); 

                }else{ 

                    Toast.makeText(MainActivity.this, R.string.run_n_note, Toast.LENGTH_SHORT).show(); 

                } 

            } 

        } 

         

        class ReadCheckBoxListener implements OnCheckedChangeListener{ 

 

      &nbs