【问题标题】:Android: How do I enable/disable a Checkbox, depending on a Radio button being selected firstAndroid:如何启用/禁用复选框,具体取决于首先选择的单选按钮
【发布时间】:2012-03-23 17:47:26
【问题描述】:

基本上我有一个带有两个单选按钮的 Radio Group,其中一个标记为 RUN,另一个标记为 PASS。 在此下方,我还有一个标记为“通过完成”的复选框

问题:如何在选择 RUN 单选按钮时禁用复选框(因此无法选择复选框)并在选择 PASS 单选按钮时启用它? 任何帮助构建某种类型的 IF 语句将不胜感激。

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


}
};

【问题讨论】:

  • 请至少阅读基本的 Java 手册和一些 Android SDK

标签: java android android-layout


【解决方案1】:

我已经设法通过更多研究来回答我的问题,也可能对其他人有所帮助......

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

【讨论】:

  • 请接受你自己的答案:)
【解决方案2】:

试试这个:

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

【讨论】:

  • 感谢您的意见,但我已经设法回答了我自己的问题
【解决方案3】:

使用 Radiogroup.setOnCheckedChangeListener,然后在检查更改时使用 checkbox.setenable

【讨论】:

    【解决方案4】:

    您可能希望在复选框中添加一个侦听器,它将检查选择并根据需要更新其他视图。

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

    【讨论】:

      【解决方案5】:

      试试这个

        final CheckBox cl_chk = (CheckBox) view.findViewById(R.id.cl_chk);
        cl_chk.setEnabled(false);
      

      这将帮助你...

      【讨论】:

        猜你喜欢
        • 2015-08-02
        • 1970-01-01
        • 2019-08-16
        • 1970-01-01
        • 2017-12-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-18
        相关资源
        最近更新 更多