在Android中以编程方式实现单选按钮的勾选和取消勾选

3
我的要求是能够从可用选项中仅检查一个单选按钮。
我的布局很简单。
用户应该只能选择p1、p2和p3中的一个。
按钮的ID为rB1、rB2和rB3。
PS:我知道可以使用RadioGroup实现相同的功能,但在这种特定情况下,我想使用Radio Button,因此需要查询。

单选按钮如何被选中?通过点击一个按钮。 - Rod_Algonquin
http://developer.android.com/reference/android/widget/RadioGroup.html - TheOnlyJakobob
@Rod_Algonquin 更新了查询,现在只需要选择一个按钮。默认情况下,它允许选择多个按钮。 - misguided
你需要使用 RadioGroup 来完成这个任务,在 RadioGroup 中声明你的 RadioButtons。 - Pratik Dasa
@AmanSingh 伙计,由于某些限制,我在这种情况下不想使用单选按钮组。虽然我理解你的想法,那肯定更容易。 - misguided
5个回答

6
尝试这个......
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


0

解决方案可以通过在 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,看看我的答案。也许那会对你有所帮助。 - Hariharan

0
尝试以下代码:-

Java

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示例。我正在寻找单选按钮一。 - misguided
@misguided RadioGroup类用于一组单选按钮。如果我们选中属于同一组的单选按钮之一,则会自动取消先前选中的任何单选按钮。 - duggu

0

尝试这种方式:

<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>

0
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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接