Java Android:如何启用/禁用复选框,具体取决于首先选择的单选按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9844061/
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
Android: How do I enable/disable a Checkbox, depending on a Radio button being selected first
提问by silver_c
Basically I have a Radio Group with two radio buttons, one of them is labelled RUN and the other is labelled PASS. Just underneath this I also have a check-box labelled "Pass Complete"
基本上我有一个带有两个单选按钮的无线电组,其中一个标记为 RUN,另一个标记为 PASS。就在这下面,我还有一个标记为“通过完成”的复选框
Question: How do I disable the check-box when RUN radio button is selected (so the checkbox can't be selected) and enable it while the PASS radio button is selected? Any help constructing some type of IF statement would be much appreciated.
问题:如何在选择 RUN 单选按钮时禁用复选框(因此无法选择该复选框)并在选择 PASS 单选按钮时启用它?任何构建某种类型的 IF 语句的帮助将不胜感激。
XML
XML
<RadioGroup
android:id="@+id/runandpass"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
androidrientation="horizontal">
<RadioButton
android:id="@+id/radiobuttonrun"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="RUN" android:layout_weight="50"/>
<RadioButton
android:id="@+id/radiobuttonpass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PASS"
android:layout_weight="50"/>
</RadioGroup>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
androidrientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:text="" />
<CheckBox
android:id="@+id/checkBoxcmpltpass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pass Complete"
android:layout_weight="6"/>
</LinearLayout>
JAVA
爪哇
package com.aces.acesfootballuk;
import android.app.Activity;
import android.os.Bundle;
public class CoachesPage extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.coachespage);
}
};
回答by Alix Bloom
Use Radiogroup.setOnCheckedChangeListener, then on checked change use checkbox.setenable
使用 Radiogroup.setOnCheckedChangeListener,然后在检查更改时使用 checkbox.setenable
回答by Laith Alnagem
You probably will want to add a listener to your checkbox, that will check for the selection and update the other views as needed.
您可能希望在复选框中添加一个侦听器,它将检查选择并根据需要更新其他视图。
captureGrp = (RadioGroup) findViewById(R.id.runandpass);
myRadio1 = (RadioButton) findViewById(R.drawable.radio1);
myRadio2 = (RadioButton) findViewById(R.drawable.radio2);
myRadio3 = (RadioButton) findViewById(R.drawable.radio3);
captureGrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.radio1) {
//disable and enable as needed
} else if (checkedId == R.id.radio2) {
// disable as needed
} else {
// add as many as you need
}
}
回答by ρяσ?ρ?я K
try this :
尝试这个 :
mRadioGroup1 = (RadioGroup) findViewById(R.id.runandpass);
mRadio1 = (RadioButton) findViewById(R.id.radiobuttonrun);
mRadio2 = (RadioButton) findViewById(R.id.radiobuttonpass);
mcheckBoxcmpltpass= (RadioButton) findViewById(R.id.checkBoxcmpltpass);
/*RadioGroup?OnCheckedChangeListener???*/
mRadioGroup1.setOnCheckedChangeListener(mChangeRadio);
}
private RadioGroup.OnCheckedChangeListener mChangeRadio = new
RadioGroup.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
// TODO Auto-generated method stub
if(checkedId==mRadio1.getId())
{
/*?mRadio1*/
mcheckBoxcmpltpass.disabled=false;
}
else if
(checkedId==mRadio2.getId())
{
/*?mRadio2*/
mcheckBoxcmpltpass.disabled=true;
}
}
};
回答by silver_c
I've managed to answer my question with a little more research, may help others as well...
我已经设法通过更多的研究来回答我的问题,也可以帮助其他人......
private void initialize() {
// TODO Auto-generated method stub
RadioGroup1 = (RadioGroup) findViewById(R.id.runandpass);
Radio1 = (RadioButton) findViewById(R.id.radiobuttonrun);
Radio2 = (RadioButton) findViewById(R.id.radiobuttonpass);
checkBoxcmpltpass = (CheckBox) findViewById(R.id.checkBoxcmpltpass);
RadioGroup1.setOnCheckedChangeListener(this);
}
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch(checkedId){
case R.id.radiobuttonrun:
checkBoxcmpltpass.setEnabled(false);
break;
case R.id.radiobuttonpass:
checkBoxcmpltpass.setEnabled(true);
break;
}
}
回答by parth parikh
try this
尝试这个
final CheckBox cl_chk = (CheckBox) view.findViewById(R.id.cl_chk);
cl_chk.setEnabled(false);
This will help you out...
这将帮助你...