2014-06-16 27 views

回答

0

你必須使用的,而不是複選框,單選按鈕,並嘗試下面的代碼: -

的Java

radioSexGroup = (RadioGroup) findViewById(R.id.radioSex); 
    btnDisplay = (Button) findViewById(R.id.btnDisplay); 

    btnDisplay.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 

       // get selected radio button from radioGroup 
      int selectedId = radioSexGroup.getCheckedRadioButtonId(); 

      // find the radiobutton by returned id 
       radioSexButton = (RadioButton) findViewById(selectedId); 

      Toast.makeText(MyAndroidAppActivity.this, 
       radioSexButton.getText(), Toast.LENGTH_SHORT).show(); 

     } 

    }); 

XML

<RadioGroup 
     android:id="@+id/radioSex" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <RadioButton 
      android:id="@+id/radioMale" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/radio_male" 
      android:checked="true" /> 

     <RadioButton 
      android:id="@+id/radioFemale" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/radio_female" /> 

    </RadioGroup> 

見下面的鏈接: -

http://www.mkyong.com/android/android-radio-buttons-example/

+0

Golu我看到了這個例子我想把字符串中的單選按鈕的文本 String Male = ..... Button – user3709878