android中的radiobutton不起作用

时间:2016-09-25 12:54:34

标签: java android radio-button

在这个程序中我使用radiobuttons。目前我的按钮编号2(button2)存在一些问题,实际上按下时没有任何反应!

当我按button2时,它应显示" TextView"中每个单选按钮的名称。 (button1用于清除选择单选按钮)。 有谁可以帮助我或给我任何提示?感谢。

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

public class RadiobuttonActivity extends Activity {     

    Button      button1;
    Button      button2;
    TextView    textView1;
    RadioButton r1;
    RadioButton r2;
    RadioButton r3;     

    RadioGroup  radioGroup1;     

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        textView1 = (TextView) findViewById(R.id.textView1);
        radioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1);
        r1 = (RadioButton) findViewById(R.id.r1);
        r2 = (RadioButton) findViewById(R.id.r2);
        r3 = (RadioButton) findViewById(R.id.r3);     

        button1.setOnClickListener(new OnClickListener() {     

            @Override
            public void onClick(View arg0) {
                radioGroup1.clearCheck();
                textView1.setText("AllUnChecked");     
            }
        });     

        button2.setOnClickListener(new OnClickListener() {     

            @Override
            public void onClick(View arg0) {
                int selectedid = radioGroup1.getCheckedRadioButtonId();
                switch (selectedid) {
                    case 0:
                        textView1.setText("Red");
                        break;
                    case 1:
                        textView1.setText("Green");
                        break;
                    case 2:
                        textView1.setText("Black");
                        break;
                }
            }
        });     
    }
}

这是main.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"
        android:orientation="vertical" >

        <RadioGroup
            android:id="@+id/radioGroup1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >



            <RadioButton
                android:id="@+id/r1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="Red" />



            <RadioButton
                android:id="@+id/r2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Green" />



            <RadioButton
                android:id="@+id/r3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Black" />

        </RadioGroup>


        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="AllUnchecked" />


        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Witch radio button is ckecked?" />


        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Text View"
            android:textAppearance="?android:attr/textAppearanceLarge" />

    </LinearLayout>

and this is main.xml file:

<?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"
    android:orientation="vertical" >

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >



        <RadioButton
            android:id="@+id/r1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="Red" />



        <RadioButton
            android:id="@+id/r2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Green" />



        <RadioButton
            android:id="@+id/r3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Black" />

    </RadioGroup>


    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="AllUnchecked" />


    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Witch radio button is ckecked?" />


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text View"
        android:textAppearance="?android:attr/textAppearanceLarge" />

1 个答案:

答案 0 :(得分:1)

this SO post

中所述

而不是使用:

int selectedid = radioGroup1.getCheckedRadioButtonId();

使用此:

 int selectedid = radioGroup1.indexOfChild(findViewById(radioGroup1.getCheckedRadioButtonId()));