【问题标题】:how to disable the radio group until checkbox is selected如何在选中复选框之前禁用单选组
【发布时间】:2015-02-20 07:03:50
【问题描述】:

我正在尝试设计一个应用,其中包含一个复选框列表,后跟单选组(每个包含 4 个单选按钮)。

概念是,在我们选择特定复选框之前,它的相关无线电组应该被禁用。我尝试了

提供的所有解决方案

How to disable a RadioGroup until checkbox is checked

Android: How do I enable/disable a Checkbox, depending on a Radio button being selected first

Android disable radio group in xml

http://developer.android.com/reference/android/widget/RadioGroup.html 但它们都不适合我..应用程序崩溃了..

这是我的代码:

public class Service extends Activity 
{    
    private String op1=" ",op2=" ";    
    private CheckBox ck1;
    private CheckBox ck2;  
    private RadioGroup rg1;
    private RadioGroup rg2;

    protected void onCreate(Bundle savedInstanceState) 
    {    
        super.onCreate(savedInstanceState);    
        setContentView(R.layout.activity_service); 

        ck1 = (CheckBox)findViewById(R.id.checkBox1);  
        ck2 = (CheckBox)findViewById(R.id.checkBox2);

        RadioGroup rg1 = (RadioGroup) findViewById(R.id.radioGroup1);    
        RadioGroup rg2 = (RadioGroup) findViewById(R.id.radioGroup2);  

        rg1.setOnCheckedChangeListener(new OnCheckedChangeListener()    
        {    
            public void onCheckedChanged(RadioGroup group, int checkedId) 
            {    
                switch(checkedId)
                {     
                    case R.id.radio1:    
                        op1="Normal";    
                        break;    
                    case R.id.radio2:    
                        op1="Extra";    
                        break;    
                }    

            }    
        });

        rg2.setOnCheckedChangeListener(new OnCheckedChangeListener()    
        {    
            public void onCheckedChanged(RadioGroup group, int checkedId) 
            {    
                switch(checkedId)
                {    
                    case R.id.radio3:    
                        op2="Normal";    
                        break;    
                    case R.id.radio4:    
                        op2="Extra";    
                        break;    
                }    
            }    
        });    
    }

    public void onCheckboxClicked(View view) 
    {     
        ck1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() 
        {    
            public void onCheckedChanged(CompoundButton checkBox, boolean isChecked) 
            {    
                RadioGroup rg1 = (RadioGroup) findViewById(R.id.radioGroup1);    
                for(int i = 0; i < rg1.getChildCount(); i++)
                {    
                    ((RadioGroup)rg1.getChildAt(i)).setEnabled(isChecked);    
                }    
            }    
        });    
        for(int i = 0; i < rg1.getChildCount(); i++)
        {    
            ((RadioGroup)rg1.getChildAt(i)).setEnabled(false);    
        }    
        }    
        ck2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() 
        {    
            public void onCheckedChanged(CompoundButton checkBox, boolean isChecked) 
            {    
                RadioGroup rg2 = (RadioGroup) findViewById(R.id.radioGroup2);    
                for(int i = 0; i < rg2.getChildCount(); i++)
                {    
                    ((RadioGroup)rg2.getChildAt(i)).setEnabled(isChecked);   
                }    
            }    
        });    
        for(int i = 0; i < rg2.getChildCount(); i++)
        {    
            ((RadioGroup)rg2.getChildAt(i)).setEnabled(false);    
        }    
    }
}

.xml 代码:

    <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="3" >

    <CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="onCheckboxClicked"
    android:text="@string/Cheese"  />

    <RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"   
    android:weightSum="2"        >

    <RadioButton
    android:id="@+id/radio1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:text="@string/Normal   />

    <RadioButton
    android:id="@+id/radio2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:text="@string/Extra  />
    </RadioGroup>
    </LinearLayout>

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <CheckBox
    android:id="@+id/checkBox2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="onCheckboxClicked"
    android:text="@string/Ketchup"  />

    <RadioGroup
    android:id="@+id/radioGroup2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" 
    android:weightSum="2"        >

    <RadioButton
    android:id="@+id/radio3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"  
    android:text="@string/Normal   />

    <RadioButton
      android:id="@+id/radio4"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_weight="0.5" 
      android:text="@string/Extra    />
    </RadioGroup>
    </LinearLayout>

如果再次取消选中复选框,则删除单选按钮

     if(isChecked)  //in switch-case
        //task
     else
        radiogroup.clearCheck();

【问题讨论】:

  • 请格式化您的代码。它不可能读这个!
  • 对不起..它不接受代码..给出错误..比如添加 4 个空格,ese cntrl+k 和所有
  • 实际上这是我的第一个问题。所以,在给出代码时遇到问题......它显示了一些规则
  • 有人编辑了该问题,请接受该编辑。
  • 首先,我建议你更改Activity的名称。服务是 Android 的构建块。不要将其用作活动的名称。

标签: android eclipse checkbox radio-group


【解决方案1】:

首先,永远不要在你声明它们的地方初始化你的类级 GUI 组件。这些元素是上下文相关的,因此它们需要创建它们的上下文。在此之前,您对findViewById 的调用将返回null。 所以把你的初始化放到onCreate方法中。

第二个问题:你需要你的 rg1 和 rg2 成员也是类成员,因为你也从onCreate 方法之外引用它们。

第三个问题:在关闭类定义的onCreate 方法之后有一个早期的右括号。

对你有用的是:

public class Service extends Activity 
{    
    private String op1=" ",op2=" ";    
    private CheckBox ck1;   
    private CheckBox ck2;    
    private RadioGroup rg1;
    private RadioGroup rg2;

    protected void onCreate(Bundle savedInstanceState) 
    {    
        super.onCreate(savedInstanceState);    
        setContentView(R.layout.activity_service);

        ck1 = (CheckBox)findViewById(R.id.checkBox1);
        ck2 = (CheckBox)findViewById(R.id.checkBox2);

        rg1 = (RadioGroup) findViewById(R.id.radioGroup1);    
        rg2 = (RadioGroup) findViewById(R.id.radioGroup2);

        rg1.setOnCheckedChangeListener(new OnCheckedChangeListener()    
        {    
            public void onCheckedChanged(RadioGroup group, int checkedId) 
            {    
                switch(checkedId)
                {     
                    case R.id.radio1:    
                        op1="Yes";    
                        break;    
                    case R.id.radio2:    
                        op1="No";    
                        break;    
                }    

            }    
        });

        rg2.setOnCheckedChangeListener(new OnCheckedChangeListener()    
        {    
            public void onCheckedChanged(RadioGroup group, int checkedId) 
            {    
                switch(checkedId)
                {    
                    case R.id.radio3:    
                        op1="Yes";    
                        break;    
                    case R.id.radio4:    
                        op1="No";    
                        break;    
                }    
            }    
        });    
    }

    public void onCheckboxClicked(View view) 
    {     
        ck1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() 
        {    
            public void onCheckedChanged(CompoundButton checkBox, boolean isChecked) 
            {    
                RadioGroup rg1 = (RadioGroup) findViewById(R.id.radioGroup4);    
                for(int i = 0; i < rg1.getChildCount(); i++)
                {    
                    ((RadioGroup)rg1.getChildAt(i)).setEnabled(isChecked);    
                }    
            }    
        });    
        for(int i = 0; i < rg1.getChildCount(); i++)
        {    
            ((RadioGroup)rg1.getChildAt(i)).setEnabled(false);    
        }    
        ck2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() 
        {    
            public void onCheckedChanged(CompoundButton checkBox, boolean isChecked) 
            {    
                RadioGroup rg2 = (RadioGroup) findViewById(R.id.radioGroup5);    
                for(int i = 0; i < rg2.getChildCount(); i++)
                {    
                    ((RadioGroup)rg2.getChildAt(i)).setEnabled(isChecked);   
                }    
            }    
        });    
        for(int i = 0; i < rg2.getChildCount(); i++)
        {    
            ((RadioGroup)rg2.getChildAt(i)).setEnabled(false);    
        }    
    }
}

另请注意,onCheckboxClicked 可能设计为直接从布局中引用,因此请确保正确绑定它。

更新基于新信息:

为了完成你的任务,我建议你使用更简单的方法,使用专用的事件监听器:

Service.java

public class Service extends Activity implements 
    android.widget.CompoundButton.OnCheckedChangeListener, 
    android.widget.RadioGroup.OnCheckedChangeListener
{
    private String op1 = " ", op2 = " ";
    private CheckBox ck1;
    private CheckBox ck2;
    private RadioGroup rg1;
    private RadioGroup rg2;

    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service);

        ck1 = (CheckBox) findViewById(R.id.checkBox1);
        ck2 = (CheckBox) findViewById(R.id.checkBox2);

        // use the same listener for each checkbox
        ck1.setOnCheckedChangeListener(this);
        ck2.setOnCheckedChangeListener(this);

        // use the same listener for each radiogroup 
        rg1 = (RadioGroup) findViewById(R.id.radioGroup1);
        rg2 = (RadioGroup) findViewById(R.id.radioGroup2);

        rg1.setOnCheckedChangeListener(this);
        rg2.setOnCheckedChangeListener(this);
    }

    // for checkbox changes:
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {
        switch (buttonView.getId())
        {
            case R.id.checkBox1:
                for (int i = 0; i < rg1.getChildCount(); i++)
                {
                    rg1.getChildAt(i).setEnabled(isChecked);
                }
                break;
            case R.id.checkBox2:
                for (int i = 0; i < rg2.getChildCount(); i++)
                {
                    rg2.getChildAt(i).setEnabled(isChecked);
                }
                break;
            default:
                break;
        }
    }

    // for radiogroup changes: 
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId)
    {
        switch (checkedId)
        {
            case R.id.radio1:
                op1 = "Yes";
                break;
            case R.id.radio2:
                op1 = "No";
                break;
            case R.id.radio3:
                op2 = "Yes";
                break;
            case R.id.radio4:
                op2 = "No";
                break;
            default:
                break;
        }
    }
}

此实现适用于以下固定布局:

activity_service.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="3" >

        <CheckBox
            android:id="@+id/checkBox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Cheese" />

        <RadioGroup
            android:id="@+id/radioGroup1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="2" >

            <RadioButton
                android:id="@+id/radio1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:checked="true"
                android:enabled="false"
                android:text="@string/Normal" />

            <RadioButton
                android:id="@+id/radio2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:enabled="false"
                android:text="@string/Extra" />
        </RadioGroup>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <CheckBox
            android:id="@+id/checkBox2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Ketchup" />

        <RadioGroup
            android:id="@+id/radioGroup2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="2" >

            <RadioButton
                android:id="@+id/radio3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:checked="true"
                android:enabled="false"
                android:text="@string/Normal" />

            <RadioButton
                android:id="@+id/radio4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:enabled="false"
                android:text="@string/Extra" />
        </RadioGroup>
    </LinearLayout>

</LinearLayout>

【讨论】:

  • awwwsssmmmm...它正在工作...非常感谢.. :) 最后它正在工作...谢谢..谢谢...谢谢...
  • @rekaszeru 如果你有时间那么请你帮我这个stackoverflow.com/questions/27602117/…
  • 不客气。请查看类结构并找出发生了什么变化(已评论);当你有时间时,你也可以改进你的布局。 :)
  • 您也可以将相同的功能应用于链接问题中的编辑文本。
  • 好的..我会试一试..但是非常感谢你.. :) :)
猜你喜欢
  • 2012-03-11
  • 2015-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-19
  • 2013-04-03
相关资源
最近更新 更多