Android - 在clearCheck()和旋转

时间:2015-07-31 18:28:30

标签: android android-radiogroup

我遇到了RadioGroups clearCheck方法和旋转我的应用程序的问题。我认为这可能是谷歌问题?

我有一个三按钮无线电组,在启动时默认选择xml中的第一个项目。

我的问题是,我有一个调用无线电组clearCheck()的按钮。如果您旋转应用程序,然后尝试按第一个无线电组项目,它将不会选择它。它尝试,它显示动画,但它仍未被选中。

我以为自己疯了,我的代码出了点问题,但我只是制作了一个新的应用程序,将其分解为最简单的应用程序。

如果我没有预先选择按钮A(通过XML或代码),那么它可以正常工作,但我需要预先选择此按钮。

我知道如何解决此问题并默认选中A?

回购步骤

  1. 启动应用
  2. 选择单选按钮B
  3. 按清除检查
  4. 旋转应用
  5. 按单选按钮A
  6. 结果:电台A不会选择。如果您按B或C,则可以再次按A,但直到那时。

    public class MainActivity extends AppCompatActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        final RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
    
        Button clearButton = (Button) findViewById(R.id.clearButton);
    
        clearButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                radioGroup.clearCheck();
            }
        });
    }
    }
    
    
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
    
     <RadioGroup
            android:id="@+id/radioGroup"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            android:gravity="center"
            android:checkedButton="@+id/radioButtonA"
            android:orientation="horizontal">
    
            <RadioButton
                android:id="@+id/radioButtonA"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="A" />
    
            <RadioButton
                android:id="@+id/radioButtonB"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="B" />
    
            <RadioButton
                android:id="@+id/radioButtonC"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="C" />
    
        </RadioGroup>
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Clear"
            android:id="@+id/clearButton"
            android:layout_below="@+id/searchRadioGroup"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="114dp" />
    
    </RelativeLayout>
    

2 个答案:

答案 0 :(得分:1)

Upon rotation, the checked id is reset due to android:checkedButton="@+id/radioButtonA".

If there was a button checked, CompoundButton's onRestoreInstanceState resets the checked button again to the correct checked button.

Contrarily, if there was no button checked, buttonA's onRestoreInstanceState will reset it to unchecked. RadioGroup is not handling this scenario because it is not designed to handle unchecks.

So, we end up with RadioGroup tracking buttonA as checked and buttonA tracking itself as unchecked. When you click on buttonA, the check is processed, however the UI does not update due to shortcircuit logic in the check() method because it assumes that since the id already matches the mCheckedId it has on hand, the button should already be checked:

// don't even bother
if (id != -1 && (id == mCheckedId)) {
    return;
}

I guess this could be considered a bug, but it ultimately comes down to some strange design choices on your part. You are setting it to default to buttonA, which means that the user cannot uncheck any button under normal circumstances and then you provide a clear button to get them to a non-default state that they could not reach under normal circumstances. So, there are two possible solutions that would bring your UI design choices in to focus:

1) If you want there to always be a checked button, have the reset button reset to default state.

clearButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        mRadioGroup.check(R.id.radioButtonA);
    }
});

2) If you want there to possibly be no checked button, don't default to anything.

答案 1 :(得分:0)

我的App遇到了这个问题。 我作弊了 。我创建了一个新的无线电按钮,然后选择了它的可见性。 之后,我将setcheked =(true)。