如何强制RadioGroup中的ToggleButton在第二次按下时保持选中状态?

时间:2013-04-26 11:39:34

标签: android togglebutton

我的活动中有一个RadioGroup,其中包含四个带自定义背景的ToggleButtons。我希望用户能够一次选择任何一个按钮,并且永远不应该选择任何按钮,但即使它在RadioGroup中并且所有其他工作正常,选择已经选择的ToggleButton也会取消选择它,按钮被选中。

如果用户再次点击ToggleButton,如何强制ToggleButton保持选中状态?

我的XML:

<RadioGroup
    android:id="@+id/radio_group"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="horizontal" />

onCreate()的相关块:

radioGroup = (RadioGroup) findViewById(R.id.radio_group);
radioGroup.setOnCheckedChangeListener(onRadioGroupClickListener);

for (int index = 0; index < OPTIONS.length; index++) {
    ToggleButton button = new ToggleButton(this);
    button.setId(index);
    button.setText(OPTIONS[index]);
    button.setTextOn(OPTION[index]);
    button.setTextOff(OPTIONS[index]);
    button.setChecked(index == 0); // Set to first option by default
    button.setButtonDrawable(Color.TRANSPARENT);
    button.setBackgroundResource(R.drawable.selector);
    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            RadioGroup parent = (RadioGroup) view.getParent();
            ToggleButton button = (ToggleButton) view;
            TimesheetLog.d("View is checked: " + button.isChecked());

            parent.check(button.getId());
            currentSelection = view.getId();
        }
    });

    radioGroup.addView(button);
}

如果我需要添加更多代码,请告诉我。谢谢!

2 个答案:

答案 0 :(得分:0)

以下行应与切换点击事件属于同一类;

private RadioButton mBtnCurrentRadio;

切换点击事件(应该在活动中);

public void onToggle(View view) {

        final ToggleButton mBtnToggle = (ToggleButton) view;

        // select only one toggle button at any given time
        if (mBtnCurrentToggle != null) {
            mBtnCurrentToggle.setChecked(false);
        }
        mBtnToggle.setChecked(true);
        mBtnCurrentToggle = mBtnToggle;

        // app specific stuff ..
    }

用于布局的RadioGroup xml代码;

<RadioGroup
        android:id="@+id/toggleGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:orientation="horizontal" >

        <ToggleButton
            android:id="@+id/btn_Letter"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:onClick="onToggle"
            android:textOff="Letter"
            android:textOn="Letter"
            style="@style/toggleBtnStyle" />

        <ToggleButton
            android:id="@+id/btn_A4"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:onClick="onToggle"
            android:textOff="A4"
            android:textOn="A4"
            style="@style/toggleBtnStyle" />

        <ToggleButton
            android:id="@+id/btn_A3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:onClick="onToggle"
            android:textOff="A3"
            android:textOn="A3"
            style="@style/toggleBtnStyle" />
    </RadioGroup>

答案 1 :(得分:0)

我有同样的问题,只是想出来了。

使用您在问题中的代码段,如果添加一行:

button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View view) {
        RadioGroup parent = (RadioGroup) view.getParent();
        ToggleButton button = (ToggleButton) view;
        TimesheetLog.d("View is checked: " + button.isChecked());

        // -------------------------------------------------
        parent.clearCheck(); // <----------- ADD THIS-------
        // -------------------------------------------------
        parent.check(button.getId());
        currentSelection = view.getId();
    }
});

我相信它应该有效。我认为发生的是check()方法实际上是一个自己的切换。如果我们每次都清除检查,那么每次我们调用check()之后都会保证​​它处于按下状态。这使得无法取消选中ToggleButton。