安卓:我如何启用/禁用复选框,取决于一个单选按钮,第一被选中 [英] Android: How do I enable/disable a Checkbox, depending on a Radio button being selected first

查看:152
本文介绍了安卓:我如何启用/禁用复选框,取决于一个单选按钮,第一被选中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我有一个单选按钮组有两个单选按钮,其中一个被标记RUN和其他标记为PASS。
下面只是这我也有一个标记复选框传递完成

问题:如何禁用该复选框当选择RUN单选按钮(这样复选框不能被选中),而选择了PASS单选按钮启用它?
任何构建某种类型的IF语句的帮助将非常AP preciated。

XML

 < RadioGroup中
机器人:ID =@ + ID / runandpass
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
androidrientation =水平><单选
机器人:ID =@ + ID / radiobuttonrun
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
安卓:检查=真
机器人:文字=RUN机器人:layout_weight =50/><单选
机器人:ID =@ + ID / radiobuttonpass
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=PASS
机器人:layout_weight =50/>
< / RadioGroup中><的LinearLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
androidrientation =水平><的TextView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =50
机器人:文字=/><复选框
机器人:ID =@ + ID / checkBoxcmpltpass
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=完全合格
机器人:layout_weight =6/>
< / LinearLayout中>

JAVA

 包com.aces.acesfootballuk;进口android.app.Activity;
进口android.os.Bundle;公共类CoachesPage延伸活动{@覆盖
保护无效的onCreate(捆绑savedInstanceState){
// TODO自动生成方法存根
super.onCreate(savedInstanceState);
的setContentView(R.layout.coachespage);
}
};


解决方案

我已经成功地回答我的问题多一点研究,可以帮助其他人也...

 私人无效的initialize(){
    // TODO自动生成方法存根
        RadioGroup1 =(RadioGroup中)findViewById(R.id.runandpass);
        收音机1 =(单选)findViewById(R.id.radiobuttonrun);
        第二广播电台=(单选)findViewById(R.id.radiobuttonpass);
        checkBoxcmpltpass =(复选框)findViewById(R.id.checkBoxcmpltpass);
        RadioGroup1.setOnCheckedChangeListener(本);
}
 公共无效onCheckedChanged(RadioGroup中组,诠释checkedId){
    // TODO自动生成方法存根
    开关(checkedId){
    案例R.id.radiobuttonrun:
        checkBoxcmpltpass.setEnabled(假);
    打破;    案例R.id.radiobuttonpass:
        checkBoxcmpltpass.setEnabled(真);
    打破;
    }
}

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"

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.

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);


}
};

解决方案

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;
    }
}

这篇关于安卓:我如何启用/禁用复选框,取决于一个单选按钮,第一被选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆