0

我试图检查一个单选按钮是否被选中,并在我发现这种我无法理解的奇怪行为时做一些事情。我必须说无线电组位于 PreferenceDialog 内,这是我对该问题的唯一解释。

RadioGroup colorRG = (RadioGroup)view.findViewById(R.id.colorRG);

         colorRG.setOnCheckedChangeListener(new OnCheckedChangeListener(){

                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    Log.w("ColorRG: ", "checkedId" + checkedId);
                    if (checkedId == (R.id.color_box1)){
                        Log.d("ColorRG: ","first if");
                        }
                    if (checkedId == (R.id.color_box1+5)){
                        Log.d("ColorRG: ","second if");
                        }
                    }
                 });

嗯,Log显示checkedId的值为2131165319,R.id.color_box1的值为2131165314,显然没有进入第一个if。它显示第二个 if 日志。

那么,这里发生了什么?

我希望有人能帮帮忙

编辑:添加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:layout_marginTop="10dp"
        android:gravity="center_horizontal" >

        <ImageView
            android:id="@+id/color_box1"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_margin="5dp"
            android:contentDescription="@string/selector_descript"
            android:src="@drawable/settings_selector_color" />

        <ImageView
            android:id="@+id/color_box2"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_margin="5dp"
            android:contentDescription="@string/selector_descript"
            android:src="@drawable/settings_selector_color" />

        <ImageView
            android:id="@+id/color_box3"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_margin="5dp"
            android:contentDescription="@string/selector_descript"
            android:src="@drawable/settings_selector_color" />

        <ImageView
            android:id="@+id/color_box4"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_margin="5dp"
            android:contentDescription="@string/selector_descript"
            android:src="@drawable/settings_selector_color" />

    </LinearLayout>

    <RadioGroup
        android:id="@+id/colorRG"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:orientation="vertical" >    

        <View
            android:layout_width="match_parent"
            android:layout_height="1dip"
            android:layout_marginLeft="4dip"
            android:layout_marginRight="4dip"
            android:background="?android:attr/dividerVertical"/>

        <RadioButton
            android:id="@+id/color_radio1"
            android:layout_marginLeft="40dp"
            android:paddingLeft="70dp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/selector_RB1" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dip"
            android:layout_marginLeft="4dip"
            android:layout_marginRight="4dip"
            android:background="?android:attr/dividerVertical"/>

        <RadioButton
            android:id="@+id/color_radio2"
            android:layout_marginLeft="40dp"
            android:paddingLeft="70dp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/selector_RB2" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dip"
            android:layout_marginLeft="4dip"
            android:layout_marginRight="4dip"
            android:background="?android:attr/dividerVertical"/>

        <RadioButton
            android:id="@+id/color_radio3"
            android:layout_marginLeft="40dp"
            android:paddingLeft="70dp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/selector_RB3" />

         <View
            android:layout_width="match_parent"
            android:layout_height="1dip"
            android:layout_marginLeft="4dip"
            android:layout_marginRight="4dip"
            android:background="?android:attr/dividerVertical"/>

        <RadioButton
            android:id="@+id/color_radio4"
            android:layout_marginLeft="40dp"
            android:paddingLeft="70dp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/selector_RB4" />
    </RadioGroup>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dip"
        android:layout_marginLeft="4dip"
        android:layout_marginRight="4dip"
        android:background="?android:attr/dividerVertical"/>

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

         <Button
            android:id="@+id/pincancel_but"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="?android:attr/selectableItemBackground"
            android:text="@string/cancelar" />

         <View
            android:layout_width="1dp"
            android:layout_height="match_parent"     
            android:layout_marginBottom="4dip"
            android:background="?android:attr/dividerVertical" />

        <Button
            android:id="@+id/pinok_but"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="?android:attr/selectableItemBackground"
            android:text="@string/ok" />
    </LinearLayout>


</LinearLayout>
4

2 回答 2

1

这不是正确的方法

checkedId == (R.id.color_box1+5)

因为id自动生成的自动生成字段R.javaYou can not create id like that. 因此,更改文件id中的第二radio buttonxml。假设你给的 id 是

android:id="@+id/color_box5"

JAVA代码将是

colorRG.setOnCheckedChangeListener(new OnCheckedChangeListener(){

                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    Log.w("ColorRG: ", "checkedId" + checkedId);
                    if (checkedId == (R.id.color_box1)){
                        Log.d("ColorRG: ","first if");
                        }
                    if (checkedId == (R.id.color_box5)){
                        Log.d("ColorRG: ","second if");
                        }
                    }
                 });

这对你有用。

于 2014-06-28T13:10:26.783 回答
1

试试这个它会帮助你

RadioGroup colorRG = (RadioGroup)view.findViewById(R.id.colorRG);    


colorRG.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(RadioGroup radioGroup,
            int radioButtonID) {

if(radioButtonID==colorRg.getCheckedRadioButtonId)
{
Log.i("tt","firstlog"+radioButtonID);//you will get id now
}
             }
    }
});
于 2014-06-28T11:34:01.730 回答