在我们选择单选按钮“是”之后如何访问android的复选框?

时间:2013-05-15 09:45:04

标签: android checkbox radio-button

我想,如果我们选择radiobutton“是”,那么我们可以使用复选框。如果我们选择radiobutton“no”,则无法使用复选框。怎么样?

其xml:

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

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <TableRow
                android:id="@+id/tableRow1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <TextView
                    android:id="@+id/textView4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textStyle="bold"
                    android:textColor="#ffffffff"
                    android:text="ARE YOU CLASS 2B?" />

            </TableRow>

            <TableRow
                android:id="@+id/tableRow2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <RadioButton
                    android:id="@+id/yes"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Yes" />

                <RadioButton
                    android:id="@+id/no"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="No" />

            </TableRow>

            <TableRow
                android:id="@+id/tableRow3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <CheckBox
                    android:id="@+id/jimmy"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Jimmy" />

            </TableRow>

            <TableRow
                android:id="@+id/tableRow4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <CheckBox
                    android:id="@+id/diana"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Diana" />

            </TableRow>

            <TableRow
                android:id="@+id/tableRow6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <CheckBox
                    android:id="@+id/dina"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Dina" />

            </TableRow>

            <TableRow
                android:id="@+id/tableRow5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <CheckBox
                    android:id="@+id/jack"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Jack" />

            </TableRow>

            <TextView
                android:id="@+id/status"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView" />

            <TableRow
                android:id="@+id/tableRow7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

            </TableRow>

        </TableLayout>

    </LinearLayout> 

它的java:

package diahp.d;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.TextView;


public class example extends Activity implements OnClickListener {

CheckBox    jimmy,diana,dina,jack;
RadioButton yes,no;
TextView status;
StringBuilder showing,showing2;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.example);

jimmy   = (CheckBox) findViewById(R.id.jimmy);
diana       = (CheckBox) findViewById(R.id.diana);
dina        = (CheckBox) findViewById(R.id.dina);
jack        = (CheckBox) findViewById(R.id.jack);
jimmy.setOnClickListener(this);
diana.setOnClickListener(this);
dina.setOnClickListener(this);
jack.setOnClickListener(this);

yes     = (RadioButton) findViewById(R.id.yes);
no      = (RadioButton) findViewById(R.id.no);
yes.setOnClickListener(this);
no.setOnClickListener(this);

status = (TextView) findViewById(R.id.status);

}

public void onClick(View v) {

     showing  = new StringBuilder();

     if(yes.isChecked()){(diana,jimmy,dina,jack==true)} //can access the checkboxes
     if(no.isChecked()){(diana,jimmy,dina,jack==false)} //can't access the checkboxes

     if(diana.isChecked()){
         showing.append("Diana");
     }
     if(jimmy.isChecked()){
         showing.append("Jimmy");
     }
     if(dina.isChecked()){
         showing.append("Dina");
     }
     if(jack.isChecked()){
         showing.append("Jack");
     }

    status.setText("Oh, you are:" +showing);


        }   }

嗯...如果我选择单选按钮“是”可以访问复选框,如果“否”则无法访问复选框。

3 个答案:

答案 0 :(得分:0)

您必须使用setOnCheckedChangeListener和更改为RadioGroup http://developer.android.com/reference/android/widget/RadioGroup.html

(radiogroup).setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

 public void onCheckedChanged(RadioGroup group, int checkedId) {


        if (checkedId == R.id.yes) {
            XY
        } else if (checkedId == R.id.no) {
            XY
        } else {
            XY
        }
    }

希望这会有所帮助。快乐的编码!

答案 1 :(得分:0)

禁用单选按钮更改时的复选框。 更多信息link

答案 2 :(得分:0)

试试这样。

if(no.isChecked())
{
//diana.setChecked(false);
//jimmy.setChecked(false);
//dina.setChecked(false);
//jack.setChecked(false);
diana.setEnabled(false);
jimmy.setEnabled(false);
dina.setEnabled(false);
jack.setEnabled(false);
}

或者使用RadioGroup

radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

    public void onCheckedChanged(RadioGroup arg0, int arg1) {

                radioButton = (RadioButton) findViewById(radioGroup
                        .getCheckedRadioButtonId());                
                if (radioButton.getText().equals("Yes")) {
                        //diana.setChecked(true);
                        //jimmy.setChecked(true);
                        //dina.setChecked(true);
                        //jack.setChecked(true);
                          diana.setEnabled(true);
                              jimmy.setEnabled(true);
                              dina.setEnabled(true);
                              jack.setEnabled(true);
                } else {
                        //diana.setChecked(false);
                        //jimmy.setChecked(false);
                        //dina.setChecked(false);
                        //jack.setChecked(false);
                              diana.setEnabled(false);
                              jimmy.setEnabled(false);
                           dina.setEnabled(false);
                              jack.setEnabled(false);
            }
            }
});

我希望这会对你有所帮助。