萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 安卓開發 >> Android開發之選項組件

Android開發之選項組件

一、基礎知識:

單項選擇組件 :  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

copyright © 萬盛學電腦網 all rights reserved