android自定义单选按钮没有被检查 [英] android custom radio button not getting checked

查看:32
本文介绍了android自定义单选按钮没有被检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了具有自定义布局的单选组,即自定义按钮.

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();

我的问题是单选按钮没有被点击.我认为是因为 android:button="@null" 所以我用 android:button="@drawable/star_dis" 替换了它,但仍然没有被点击.>

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.

推荐答案

与我的代码唯一不同的是我没有将 clickable="true" 放在 xml 中,如果 RadioGroup 自己处理触摸事件以管理子单选按钮,您应该删除该属性,因为当您设置 OnClickListener 时,通常会自动将 clickable 设置为 true.

Only difference to my code is that I don't put clickable="true" in xml, if RadioGroup handles touch event by itself to manage child radio buttons you should remove that attribute as clickable is generally set to true automatically when you set an OnClickListener.

这篇关于android自定义单选按钮没有被检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆