如何在选中复选框之前禁用无线电组

时间:2014-12-22 07:28:08

标签: android eclipse checkbox radio-group

我正在尝试设计一个应用程序,其中包含一个复选框列表,后跟无线电组(每个包含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>

如果再次取消选中复选框,则删除radiobutton

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

1 个答案:

答案 0 :(得分:2)

首先,永远不要初始化声明它们的类级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可能是设计为直接从布局引用,因此请确保正确绑定它。

根据新信息

更新

为了完成你的任务,我建议你使用一个更简单的方法,使用专门的eventlisteners:

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