在radiobutton android中区分检查和点击。

问题描述 投票:0回答:1

我正在使用RadioGroup,里面有两个单选按钮。我想在点击radiobutton时显示一个带按钮的对话框,但不检查radiobutton,只有当对话框内的按钮被按下时才检查。使用 onCheckedChangeListener() 已经检查radiobutton。我试图在用户应用之前显示主题的预览,如何处理这种情况? 我怎样才能处理这种情况?

<RadioGroup
    android:id="@+id/checkButtonGroup"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:checkedButton="@+id/first"
    android:orientation="horizontal">
    <RadioButton
        android:id="@+id/first"
        android:layout_width="0dp"
        android:layout_weight="0.5"
        android:layout_height="100dp"
        android:background="@drawable/theme_light_selector"
        android:layout_margin="10dp"
        android:button="@null"
        android:text="Radio 1"/>
    <RadioButton
        android:id="@+id/second"
        android:layout_width="0dp"
        android:layout_weight="0.5"
        android:background="@drawable/theme_light_selector"
        android:layout_height="100dp"
        android:layout_margin="10dp"
        android:button="@null"
        android:text="Radio 2"/>
</RadioGroup>
android android-radiobutton
1个回答
0
投票

在Radiobutton上使用setOnClickListener()来实现这个目的,就像我在下面实现的一个radio按钮一样。

 RadioButton first= findViewById(R.id.first);
    first.setOnClickListener (new View.OnClickListener () {
        @Override
        public void onClick(View v) {
            new AlertDialog.Builder(Testing.this)
                    .setTitle("Change theme?")
                    .setMessage("Are you sure you want to change theme of button?")

                    // Specifying a listener allows you to take an action before dismissing the dialog.
                    // The dialog is automatically dismissed when a dialog button is clicked.
                    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                        first.setChecked (true);
                        first.setBackground (getDrawable (R.drawable.your_no_theme_name));

                        }
                    })


                    .setNegativeButton (android.R.string.no, new DialogInterface.OnClickListener () {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            first.setChecked (false);
                            first.setBackground (getDrawable (R.drawable.your_yes_theme_name));
                        }
                    })
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .show();
        }
    });
© www.soinside.com 2019 - 2024. All rights reserved.