我的要求是能够从可用选项中仅检查一个单选按钮。
我的布局很简单。
用户应该只能选择p1、p2和p3中的一个。
按钮的ID为rB1、rB2和rB3。
PS:我知道可以使用RadioGroup实现相同的功能,但在这种特定情况下,我想使用Radio Button,因此需要查询。
我的布局很简单。
用户应该只能选择p1、p2和p3中的一个。
按钮的ID为rB1、rB2和rB3。
PS:我知道可以使用RadioGroup实现相同的功能,但在这种特定情况下,我想使用Radio Button,因此需要查询。
r1.setOnCheckedChangeListener(this);
r2.setOnCheckedChangeListener(this);
r3.setOnCheckedChangeListener(this);
And CheckedChangeListener
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
if (buttonView.getId() == R.id.rB1) {
r2.setChecked(false);
r3.setChecked(false);
}
if (buttonView.getId() == R.id.rB2) {
r1.setChecked(false);
r3.setChecked(false);
}
if (buttonView.getId() == R.id.rB3) {
r1.setChecked(false);
r2.setChecked(false);
}
}
}
不要忘记在您的活动或片段中实现 OnCheckedChangeListener
。
解决方案可以通过在 layout.xml
文件中设置 android:onClick="onRadioButtonClick"
并在 Activity.java
中创建一个 onRadioButtonClick
方法来实现
public void onRadioButtonClick(View v)
{
boolean checked = ((RadioButton) v).isChecked();
RadioButton r1 = (RadioButton) findViewById(R.id.rB1);
RadioButton r2 = (RadioButton) findViewById(R.id.rB2);
RadioButton r3 = (RadioButton) findViewById(R.id.rB3);
switch(v.getId()) {
case R.id.rB1:
if (checked){
r2.setChecked(false);
r3.setChecked(false);
}
break;
case R.id.rB2:
if (checked){
r1.setChecked(false);
r3.setChecked(false);
}
break;
case R.id.rB2:
if (checked){
r1.setChecked(false);
r2.setChecked(false);
}
break;
}
CheckedChangeListener
,看看我的答案。也许那会对你有所帮助。 - HariharanJava
public class MyAndroidAppActivity extends Activity {
private RadioGroup radioSexGroup;
private RadioButton radioSexButton;
private Button btnDisplay;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<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>
<Button
android:id="@+id/btnDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_display" />
</LinearLayout>
更多细节请参见下面的链接:
http://www.mkyong.com/android/android-radio-buttons-example/
http://www.tutorialspoint.com/android/android_radiogroup_control.htm
尝试这种方式:
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/p1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="P1" />
<RadioButton
android:id="@+id/p2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="P2" />
<RadioButton
android:id="@+id/p3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="P3" />
</RadioGroup>
Use Radio group
XML:
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/rB1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="P1"
android:checked="true" />
<RadioButton
android:id="@+id/rB2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="P2" />
<RadioButton
android:id="@+id/rB3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="P3" />
</RadioGroup>