萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 安卓開發 >> 安卓單項選擇技巧介紹

安卓單項選擇技巧介紹

大家知道安卓單項選擇嗎?下面我們就給大家詳細介紹一下吧!

  1. public class RadioActivity extends Activity {
  2.  
  3.     Context mContext = null;
  4.     @Override
  5.     protected void onCreate(Bundle savedInstanceState) {
  6.         setContentView(R.layout.radioview);
  7.         mContext = this;
  8.         //單選組(只有在一個組中的按鈕可以單選)
  9.         RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radion0);
  10.         
  11.         //單選按鈕(第一組)
  12.         final RadioButton radioButton0 = (RadioButton)findViewById(R.id.radionButton0);
  13.         final RadioButton radioButton1 = (RadioButton)findViewById(R.id.radionButton1);
  14.         final RadioButton radioButton2 = (RadioButton)findViewById(R.id.radionButton2);
  15.         
  16.         radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  17.             
  18.             @Override
  19.             public void onCheckedChanged(RadioGroup arg0, int checkID) {
  20.                 if(radioButton0.getId() == checkID) {
  21.                     Toast.makeText(RadioActivity.this, "您選中了第一組" + radioButton0.getText(), Toast.LENGTH_LONG).show();
  22.                 }else if(radioButton1.getId() == checkID) {
  23.                     Toast.makeText(RadioActivity.this, "您選中了第一組" + radioButton1.getText(), Toast.LENGTH_LONG).show();
  24.                 }else if(radioButton2.getId() == checkID) {
  25.                     Toast.makeText(RadioActivity.this, "您選中了第一組" + radioButton2.getText(), Toast.LENGTH_LONG).show();
  26.                 }
  27.             }
  28.         });
  29.         
  30.         RadioGroup radioGroup0 = (RadioGroup)findViewById(R.id.radion1);
  31.         
  32.         //單選按鈕(第二組)
  33.         final RadioButton radioButton3 = (RadioButton)findViewById(R.id.radionButton3);
  34.         final RadioButton radioButton4 = (RadioButton)findViewById(R.id.radionButton4);
  35.         final RadioButton radioButton5 = (RadioButton)findViewById(R.id.radionButton5);
  36.         
  37.         radioGroup0.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  38.             
  39.             @Override
  40.             public void onCheckedChanged(RadioGroup arg0, int checkID) {
  41.                 if(radioButton3.getId() == checkID) {
  42.                     Toast.makeText(RadioActivity.this, "您選中了第二組" + radioButton3.getText(), Toast.LENGTH_LONG).show();
  43.                 }else if(radioButton4.getId() == checkID) {
  44.                     Toast.makeText(RadioActivity.this, "您選中了第二組" + radioButton4.getText(), Toast.LENGTH_LONG).show();
  45.                 }else if(radioButton5.getId() == checkID) {
  46.                     Toast.makeText(RadioActivity.this, "您選中了第二組" + radioButton5.getText(), Toast.LENGTH_LONG).show();
  47.                 }
  48.             }
  49.         });
  50.         super.onCreate(savedInstanceState);
copyright © 萬盛學電腦網 all rights reserved