【问题标题】:How can I force a ToggleButton within a RadioGroup to stay selected on a second press?如何强制 RadioGroup 中的 ToggleButton 在第二次按下时保持选中状态?
【发布时间】:2016-04-21 16:06:37
【问题描述】:

我的活动中有一个 RadioGroup,其中包含四个具有自定义背景的 ToggleButton。我希望用户能够一次选择任何一个按钮,并且永远不应该选择任何按钮,但即使它在 RadioGroup 中并且一切正常,选择已经选择的 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);
}

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

【问题讨论】:

  • 嗯,你为什么不在你的单选组中使用单选按钮?他们按照你想要的方式行事
  • 我在让任何东西工作时遇到了很多麻烦,据我所知,单选按钮看起来并不像真正的按钮。我错了吗?我以为文字总是偏题...
  • 它们看起来像单选按钮 :-) 是的,文本偏向一边。你看this question了吗?
  • 是的,这就是我基于我的代码的答案 - 唯一的问题是你可以没有选择,但我想始终保持至少一个选择。顺便说一句,感谢您的回复!

标签: android togglebutton


【解决方案1】:

下面的行应该与切换点击事件在同一个类中;

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>

【讨论】:

    【解决方案2】:

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

    使用问题中的代码 sn-p,如果添加一行:

    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 成为不可能。

    【讨论】:

      猜你喜欢
      • 2016-05-26
      • 2021-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多