当应用程序退出并启动时,如何保存先前选择的单选按钮状态并始终处于首选状态?

时间:2014-12-15 06:35:47

标签: android android-radiogroup android-radiobutton

单击按钮时,会显示弹出对话框,其中有五个单选按钮。现在的要求是

  1. 当我启动并单击按钮时,弹出对话框最初会显示第一个单选按钮,该按钮应处于检查状态,而其他按钮处于未选中状态。
  2. 当用户通过选择下一个单选按钮进行更改时,关闭对话框,如果用户打开弹出窗口,则该对话框应该处于先前选择的状态。
  3. 如何处理?

    代码如下:

    public class PopUpDialogRadioButton extends Activity {
        private Button popUpClick;
        private Dialog dialog;
        private RadioGroup radioGroup;
        private RadioButton radioButton1, radioButton2, radioButton3, radioButton4, radioButton5;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.sortfilterclick);
        popUpClick = (Button) findViewById(R.id.popupButton); // on click of button, opens a pop up dialog
        popUpClick.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
    
                dialog = new Dialog(PopUpDialogRadioButton.this);
                dialog.setContentView(R.layout.sortfilterrow);
                radioGroup = (RadioGroup) dialog.findViewById(R.id.radioGroup1);
                radioButton1 = (RadioButton) dialog.findViewById(R.id.radio1);
                radioButton2 = (RadioButton) dialog.findViewById(R.id.radio2);
                radioButton3 = (RadioButton) dialog.findViewById(R.id.radio3);
                radioButton4 = (RadioButton) dialog.findViewById(R.id.radio4);
                radioButton5 = (RadioButton) dialog.findViewById(R.id.radio5);
                radioButton1.setOnClickListener(radioButtonOnClickListener);
                radioButton2.setOnClickListener(radioButtonOnClickListener);
                radioButton3.setOnClickListener(radioButtonOnClickListener);
                radioButton4.setOnClickListener(radioButtonOnClickListener);
                radioButton5.setOnClickListener(radioButtonOnClickListener);
                radioGroup.clearCheck();     
                radioButton1.setChecked(true);
                int selectedId = radioGroup.getCheckedRadioButtonId();
                RadioButton osButton = (RadioButton) dialog
                        .findViewById(selectedId);
                StringBuffer responseText = new StringBuffer();
                responseText.append(osButton.getText());
                Toast.makeText(getApplicationContext(), responseText,
                        Toast.LENGTH_SHORT).show();
                // radioGroup.setOnCheckedChangeListener(radioGroupOnCheckedChangeListener);
    
                dialog.setCancelable(true);
                dialog.setTitle("Sort By");
                dialog.show();
            }
    
            private final OnClickListener radioButtonOnClickListener = new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    /*
                     * RadioButton rb = (RadioButton) v;
                     * Toast.makeText(SortFilterPopupActivity.this,
                     * rb.getText(), Toast.LENGTH_SHORT).show();
                     */
                    switch (v.getId()) {
                    case R.id.radio1:
                        Toast.makeText(PopUpDialogRadioButton.this, "first",
                                Toast.LENGTH_SHORT).show();
                                                dialog.dismiss();
                        break;
                    case R.id.radio2:
                        Toast.makeText(PopUpDialogRadioButton.this, "two",
                                Toast.LENGTH_SHORT).show();
                                                dialog.dismiss();
                        break;
                    case R.id.radio3:
                        Toast.makeText(PopUpDialogRadioButton.this, "three",
                                Toast.LENGTH_SHORT).show();
                                                dialog.dismiss();
                        break;
                    case R.id.radio4:
                        Toast.makeText(PopUpDialogRadioButton.this, "four",
                                Toast.LENGTH_SHORT).show();
                                            dialog.dismiss();
                        break;
                    case R.id.radio5:
                        Toast.makeText(PopUpDialogRadioButton.this, "five",
                                Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                        break;
                    }
                }
            };
        });
    }
    

2 个答案:

答案 0 :(得分:4)

您应该使用SharedPreferences

你有5个radiobuttons,所以如果你之前没有使用过对话框,你可以使用SharedPreferences和默认值(例如0)。

int radio_selected = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE).getInt("my_selected_radio",0);

然后,您可以使用OnClickListener知道选择了哪个值并存储它。

int radio_selected = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE).edit().putInt("my_selected_radio",selectedValue).commit();

这应该足以实现您期望的行为

希望它有所帮助!

答案 1 :(得分:0)

您可以将所选项目存储在SharedPreferences中。检查开发者网站是否有introduction to SharedPreferences

样本用法:

   // Loading preferences.
   SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
   String selectedItem = settings.getString("chkboxSelection");
   if (selectedItem != null) {
        // Compare value and mark checkbox accordingly.
   }

   // Saving preferences.
   SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
   SharedPreferences.Editor editor = settings.edit();
   editor.putString("chkboxSelection","checkboxIdentifier");