当检查第一个放射性组上的单选按钮时,如何在放射组外部单选按钮取消选中?

问题描述 投票:1回答:1

我有这样的情况,我有一个tablelayout有4个radiogroups,每个radiogroup有2个radiobutton。如何使4个放射组彼此同步。当检查放射线组1中的无线电按钮时,放射线组2中的无线电按钮是否未经检查?

这是我的XML代码:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
    android:layout_margin="20sp"
    android:textSize="25sp"
    android:fontFamily="monospace"
    android:layout_gravity="center_horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/judulkolomtes"/>

<TableLayout
    android:layout_margin="10sp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:background="@drawable/border_white">

        <RadioGroup
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:orientation="horizontal">
        <TextView
            android:padding="10sp"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="@string/P"
            android:textStyle="bold"
            android:textAlignment="center"
            android:textSize="18sp"
            android:textColor="@color/white"/>

        <TextView
            android:padding="10sp"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="@string/K"
            android:textStyle="bold"
            android:textAlignment="center"
            android:textSize="18sp"
            android:textColor="@color/white"/>
        </RadioGroup>

        <TextView
            android:padding="10sp"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:text="@string/pernyataanhead"
            android:textStyle="bold"
            android:textSize="18sp"
            android:textColor="@color/white"/>

    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:background="@drawable/border_black">

        <RadioGroup
            android:id="@+id/Rgroup1"
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/Pradio1"
                android:checked="false"
                android:layout_gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:background="@drawable/border_black"/>

            <RadioButton
                android:id="@+id/Kradio1"
                android:layout_gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:background="@drawable/border_black"/>
        </RadioGroup>
        <TextView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:paddingTop="2dp"
            android:text="@string/pernyataan1"
            android:textSize="19sp"
            android:textColor="@color/black"/>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/border_black"
        android:orientation="horizontal">

        <RadioGroup
            android:id="@+id/Rgroup2"
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:orientation="horizontal">

        <RadioButton
            android:layout_gravity="center"
            android:id="@+id/Pradio2"
            android:checked="false"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@drawable/border_black"/>

        <RadioButton
            android:layout_gravity="center"
            android:id="@+id/Kradio2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@drawable/border_black"/>
        </RadioGroup>
        <TextView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:paddingTop="2dp"
            android:text="@string/pernyataan2"
            android:textSize="19sp"
            android:textColor="@color/black"/>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:background="@drawable/border_black">

        <RadioGroup
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:orientation="horizontal">

        <RadioButton
            android:id="@+id/Pradio3"
            android:layout_gravity="center"
            android:checked="false"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@drawable/border_black"/>

        <RadioButton
            android:id="@+id/Kradio3"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@drawable/border_black"/>
        </RadioGroup>

        <TextView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:paddingTop="2dp"
            android:text="@string/pernyataan3"
            android:textSize="19sp"
            android:textColor="@color/black"/>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:background="@drawable/border_black">

        <RadioGroup
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:orientation="horizontal">

        <RadioButton
            android:id="@+id/Pradio4"
            android:layout_gravity="center"
            android:checked="false"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@drawable/border_black"/>

        <RadioButton
            android:id="@+id/Kradio4"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@drawable/border_black"/>
        </RadioGroup>

        <TextView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:paddingTop="2dp"
            android:text="@string/pernyataan4"
            android:textSize="19sp"
            android:textColor="@color/black"/>
    </TableRow>
</TableLayout>

我试过使用这段代码:

List<RadioButton> radioButtons = new ArrayList<RadioButton>();
    radioButtons.add( (RadioButton)findViewById(R.id.Pradio1) );
    radioButtons.add( (RadioButton)findViewById(R.id.Pradio2) );
    radioButtons.add( (RadioButton)findViewById(R.id.Pradio3) );

    for (RadioButton button : radioButtons){

        button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) processRadioButtonClick(buttonView);
            }
        });
    }

private void processRadioButtonClick(CompoundButton buttonView){

    for (RadioButton button : radioButtons){

        if (button != buttonView ) button.setChecked(false);
    }

}

但结果并不像我预期的那样。当我在放射线组1检查了无线电按钮时,我检查了放射线组2中的无线电频率。放射线组1中的无线电按钮变得不受控制,但是当我想在放射线组1中再次检查无线电按钮时,无法检查无线电按钮,同时放射线按钮在放射线组2中也是如此。这个问题有解决方案吗?根据我的分析,当检查另一个无线电按钮时,使无线电按钮失效,导致无线电按钮无法检查。

android radio-button android-radiogroup
1个回答
0
投票

这是解决方案。

public class MainActivity extends AppCompatActivity {
    List<RadioButton> radioButtons;
    List<RadioGroup> radioGroups;

    boolean flg = false;

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

        final RadioButton pradio1 = (RadioButton) findViewById(R.id.Pradio1);
        final RadioButton pradio2 = (RadioButton) findViewById(R.id.Pradio2);
        final RadioButton pradio3 = (RadioButton) findViewById(R.id.Pradio3);
        final RadioButton pradio4 = (RadioButton) findViewById(R.id.Pradio4);
        final RadioButton kradio1 = (RadioButton) findViewById(R.id.Kradio1);
        final RadioButton kradio2 = (RadioButton) findViewById(R.id.Kradio2);
        final RadioButton kradio3 = (RadioButton) findViewById(R.id.Kradio3);
        final RadioButton kradio4 = (RadioButton) findViewById(R.id.Kradio4);
        final RadioGroup rgroup1 =  (RadioGroup) findViewById(R.id.Rgroup1);
        final RadioGroup rgroup2 = (RadioGroup) findViewById(R.id.Rgroup2);
        final RadioGroup rgroup3 = (RadioGroup) findViewById(R.id.Rgroup3);
        final RadioGroup rgroup4 = (RadioGroup) findViewById(R.id.Rgroup4);

        radioButtons = new ArrayList<RadioButton>();
        radioButtons.add(pradio1);
        radioButtons.add(pradio2);
        radioButtons.add(pradio3);
        radioButtons.add(pradio4);
        radioButtons.add(kradio1);
        radioButtons.add(kradio2);
        radioButtons.add(kradio3);
        radioButtons.add(kradio4);

        radioGroups = new ArrayList<RadioGroup>();
        radioGroups.add(rgroup1);
        radioGroups.add(rgroup2);
        radioGroups.add(rgroup3);
        radioGroups.add(rgroup4);

        Log.e("JK->",""+ ((RadioGroup) pradio1.getParent()).getId());

        for (RadioButton button : radioButtons) {
            button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        Log.e("JK->","JK");
                        processRadioButtonClick(buttonView);
                    }

                }
            });
        }
    }

    private void processRadioButtonClick(CompoundButton buttonView) {
        for (RadioGroup radioGroup: radioGroups){
            if (radioGroup.getId() != ((RadioGroup) buttonView.getParent()).getId()) {
                radioGroup.clearCheck();
            }
        }
    }
}

What went wrong in your code.

在Android中无法取消选中RadioButton。您试图使用setChecked(false)取消选中RadioButton。它在内部类(Android的RadioButton类)中行为不端。它在视觉上取消选中单选按钮但不是逻辑上的(我自己虽然来自RadioButton行为)。这就是你的代码无效的原因。

Solution Explanation.

为了解决这个问题而不是使用setChecked(false)取消选中RadioButton,我使用radioGroup.clearCheck()取消选中该特定组的两个按钮(视觉和逻辑两者)。我不确定这是一个明确的解释。如果您有任何疑问,请告诉我。 :)

© www.soinside.com 2019 - 2024. All rights reserved.