我在我的活動中有一個RadioGroup,其中包含四個具有自定義背景的ToggleButton。我希望用戶能夠一次選擇任何一個按鈕,並且不應該沒有選擇任何按鈕,但即使它在RadioGroup中,並且其他所有工作都正常,選擇已選擇的ToggleButton將取消選擇它,而不會選擇任何按鈕按鈕被選中。如何強制RadioGroup中的ToggleButton在第二次按下時保持選中狀態?
如何讓用戶再次點擊ToggleButton時保持選中狀態?
我的XML:我的onCreate()的
<RadioGroup
android:id="@+id/radio_group"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal" />
相關大塊:
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);
}
如果我需要添加任何代碼,請告訴我。謝謝!
恩,你爲什麼不使用收音機組中的單選按鈕?他們的行爲是你想要的方式 – 2013-04-26 12:04:05
我有很多事情要做,並且據我所知,單選按鈕並不像真正的按鈕。我錯了嗎?我認爲文字總是偏向一邊...... – Cornholio 2013-04-26 12:25:03
他們看起來像單選按鈕:-)是的,文字是偏旁的。你看看[這個問題](http://stackoverflow.com/questions/2379527/android-how-to-get-a-radiogroup-with-togglebuttons)? – 2013-04-26 12:39:41