简体   繁体   English

在Android中选择时保存单选按钮的状态

[英]saving state of radio buttons while selection in android

I am working on and application where my activity has 2 radio buttons. 我正在开发一个有2个单选按钮的应用程序。

  • 4 channel 4通道
  • 9 channel 9通道

a - default selection of the screen is on 4channel radio button. a-屏幕的默认选择是4通道单选按钮。

b - if user want to click (9channel) button, it open a dialog box prompting user b-如果用户要单击(9channel)按钮,则会打开一个对话框,提示用户

" if you move, all saved items will be deleted " “如果您移动,所有已保存的项目都将被删除”

c - In the dialog box i have "Yes" and "No" button. c-在对话框中,我有“是”和“否”按钮。 If you click "Yes" button it moved to 9 channel radio button selection. 如果单击“是”按钮,它将移至9通道单选按钮选择。

The issues that i want to fix are : 我要修复的问题是:

1 - when user click on "No" button on the dialog box, i quit the dialog. 1-当用户单击对话框上的“否”按钮时,我退出对话框。 But selection of radio button still moves to the (9channel) radio button How to keep the old selection if user click "No" on the dialog box. 但是单选按钮的选择仍会移至(9channel)单选按钮如果用户单击对话框上的“否”,如何保持旧选择。

2 - Default selection on the activity is on (4channel) radio button. 2-活动的默认选择是(4channel)单选按钮。 If user click on the same selected button, i dont want the dialog box to open. 如果用户单击相同的选定按钮,则我不希望打开对话框。
The dialog box should open only if i click on the other radio button . 仅当我单击另一个单选按钮时,对话框才应打开。

How can i achieve the above 2 scenarios. 我如何实现以上两种情况。

my Code : 我的代码:

rg = (RadioGroup) findViewById(R.id.radioGroup1);
public void addListenerOnButton() 
{

   OnClickListener listener = new OnClickListener() {
        @Override
        public void onClick(View v) {
               RadioButton rb = (RadioButton) v;
               selection = (String) rb.getText();

                   mFlag = true;
                   showDialog(SELECTION_ALERT_ID);

        }
    };

    RadioButton rb1 = (RadioButton) findViewById(R.id.radioButton1);
    rb1.setOnClickListener(listener);
    RadioButton rb2 = (RadioButton) findViewById(R.id.radioButton2);
    rb2.setOnClickListener(listener);
} 

Problem is not in the code you show. 问题不在您显示的代码中。 In the scenario you describe, your app is focused in the dialog, so you must control the action on the dialog . 在您描述的场景中,您的应用专注于对话框,因此您必须控制对话框上的操作

First, you dont want to do any action if rb1 is pressed, so delete this line: 首先,如果按了rb1 ,则您不想执行任何操作,因此请删除此行:

rb1.setOnClickListener(listener);

After, your dialog code is not shown, so I show you the way I do this usually with some modifications to fit your needs: 之后,将不显示您的对话框代码,因此我向您展示了通常的方法,并进行了一些修改以满足您的需要:

AlertDialog dialog = new AlertDialog.Builder(this)
            // set's the message to the dialog
            .setMessage("if you move, all saved items will be deleted")
            // accept button
            .setPositiveButton(android.R.string.ok, new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                      dialog.dismiss();
                      // select the 9channel and do the actions
                }
            })
            // cancel button
            .setNegativeButton(android.R.string.cancel, new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                      dialog.dismiss();
                }
            // dismiss action (if back button is pressed, same as cancel)
            }).setOnCancelListener(new OnCancelListener() {

                @Override
                public void onCancel(DialogInterface dialog) {
                      dialog.dismiss();
                }
            }).create();

    dialog.show();

}
AlertDialog.Builder builder = new AlertDialog.Builder(ca);
            builder.setTitle("Final Cropped Image");
            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int id) {

        //check 9chanel

}
   });
           builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            // check 4chanell

                        }
                    });
            AlertDialog dialog = builder.create();
            dialog.show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM