Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
336 views
in Technique[技术] by (71.8m points)

onclick - android custom radio button not getting checked

I have created radio group with custom layout i.e. custom button.

<?xml version="1.0" encoding="utf-8"?>

<RadioGroup
    android:id="@+id/radio_group_rating"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:orientation="horizontal" >

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

        <RadioButton
            android:id="@+id/radio_platinum"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:clickable="true"
            android:drawablePadding="10dp"
            android:drawableTop="@drawable/star_small_dis"
            android:gravity="center"
            android:text="@string/platinum" />

        <RadioButton
            android:id="@+id/radio_gold"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:clickable="true"
            android:drawablePadding="10dp"
            android:drawableTop="@drawable/star_small_dis"
            android:gravity="center"
            android:text="@string/gold" />

        <RadioButton
            android:id="@+id/radio_silver"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:clickable="true"
            android:drawablePadding="10dp"
            android:drawableTop="@drawable/star_small_dis"
            android:gravity="center"
            android:text="@string/silver" />
    </LinearLayout>
</RadioGroup>

<Button
    android:id="@+id/btn_submit"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_below="@id/radio_group_rating"
    android:text="@string/submit" />

And in Activity, I have below code,

final Dialog dialog = new Dialog(mActivity);
    dialog.setContentView(R.layout.rating_cust_dialog_layout);
    radioGroupRating = (RadioGroup) dialog.findViewById(R.id.radio_group_rating);
    Button btnSubmit = (Button) dialog.findViewById(R.id.btn_submit);
    btnSubmit.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            int selectedRadioId = radioGroupRating.getCheckedRadioButtonId();
            View radioButton = radioGroupRating.findViewById(selectedRadioId);
            Integer selctedPosition = radioGroupRating.indexOfChild(radioButton);
            dialog.dismiss();
        }
    });
    dialog.show();

My issue is radio buttons are not getting clicked. I thought it's because of android:button="@null" So I replaced it with android:button="@drawable/star_dis" but still not getting clicked.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Its the android:button="@null", please remove it from the xml file completely. Try using this simple xml layout.

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

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

        <RadioButton
            android:id="@+id/radio_platinum"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="platinum" />

        <RadioButton
            android:id="@+id/radio_gold"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="gold" />

        <RadioButton
            android:id="@+id/radio_silver"
           android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="silver" />
        </RadioGroup>

    <Button
        android:id="@+id/btn_submit"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="submit" />
</LinearLayout>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...