2015-09-04 34 views
-3

我如何知道RadioButton被選中或未被選中?我如何知道RadioButton被選中或未被選中?

XML文件:

<RadioButton 
     android:id="@+id/radioButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="RadioButton" /> 

的java文件:

public class Setting extends ActionBarActivity { 

     @Override 

     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_setting); 
     } 
    } 

回答

0

如果要檢查只有一個單選按鈕可以使用器isChecked功能

if(radioButton.isChecked()) 
{ 
\\ is checked  
} 
else 
{ 
\\ not checked 
} 

,如果你有您可以使用的RadioGroup

if (radioGroup.getCheckedRadioButtonId() == -1) 
{ 
\\ no radio buttons are checked 
} 
else 
    { 
\\ one of the radio buttons is checked 
    } 
0

radiobutton.isChecked()會給你布爾值,你可以檢查它的條件。

<RadioButton 
    android:id="@+id/radioButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:onClick="check" 
    android:text="RadioButton" /> 


@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_setting); 

    RadioButton rbutton=(RadioButton) findViewById(R.id.radioButton); 
} 

public void check(View v) 
{ 
    if (radioButton.isChecked()) 
    { 
     //do something  
    } 
    else 
    { 
     //do something 
    } 
} 
相關問題