Android 当复选框改变状态时如何做某事?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11332111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to do something when a checkbox change state?
提问by hyperloris
This is my code:
这是我的代码:
<CheckBox
android:id="@+id/sprint_checkbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/sprint_game" />
<CheckBox
android:id="@+id/marathon_checkbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/marathon" />
<CheckBox
android:id="@+id/never_ending_checkbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/never_ending" />
What i want to do is "detect" when one of these is checked and then set the other two to "disable", so the user can select only one at time. I tried to use the ".setOnCheckedChangeListener", but i can't do that, can someone help me with some code? Thanks a lot guys!
我想要做的是“检测”何时检查其中一个,然后将其他两个设置为“禁用”,这样用户一次只能选择一个。我尝试使用“.setOnCheckedChangeListener”,但我不能这样做,有人可以帮我写一些代码吗?非常感谢伙计们!
回答by Stefano Ortisi
This is the way you are notified about checked changes:
这是您收到有关已检查更改的通知的方式:
CheckBox check = findViewById(R.id.sprint_checkbox);
check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//do stuff
}
});
You can also let your activity implements the OnCheckedChangeListener interface and then:
您还可以让您的活动实现 OnCheckedChangeListener 接口,然后:
CheckBox check1 = findViewById(R.id.sprint_checkbox);
CheckBox check2 = findViewById(R.id.marathon_checkbox);
CheckBox check3 = findViewById(R.id.never_ending_checkbox);
check1.setOnCheckedChangeListener(this);
check2.setOnCheckedChangeListener(this);
check3.setOnCheckedChangeListener(this);
Overriding the interface method:
重写接口方法:
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch(buttonView.getId()){
case R.id.sprint_checkbox:
//do stuff
break;
case R.id.marathon_checkbox:
//do stuff
break;
case R.id.never_ending_checkbox:
//do stuff
break;
}
}
回答by Erol
I believe a RadioButton would be more suitable for your aims.
我相信 RadioButton 会更适合您的目标。
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/radio_group1">
<RadioButton
android:id="@+id/sprint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/rad_option1"
android:checked="true"
android:onClick="onRadioButtonClick"
/>
<RadioButton
android:id="@+id/marathon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/rad_option2"
android:onClick="onRadioButtonClick"
/>
<RadioButton
android:id="@+id/neverending"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/rad_option3"
android:onClick="onRadioButtonClick"
/>
</RadioGroup>
Then in your code:
然后在你的代码中:
public void onRadioButtonClick(View v) {
RadioButton button = (RadioButton) v;
// DO YOUR THING HERE DEPENDING ON WHICH RADIOBUTTON IS PICKED
}
In this case, you won't have to deal with disabling other options. The user will have only one option to pick by default.
在这种情况下,您不必处理禁用其他选项的问题。默认情况下,用户只能选择一个选项。
回答by Twahanz
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int id = buttonView.getId();
if(isChecked){
switch(id){
case R.id.sprint:
//add to database
...
}
}
else{
switch(id){
case R.id.sprint:
//remove from database
...
}
}
}
This should allow you to with ID's. Hope it works.
这应该允许您使用 ID。希望它有效。