2

当我单击一个按钮时,它会显示包含五个单选按钮的弹出对话框。现在要求是

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

如何进行此操作?

代码如下:

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;
                }
            }
        };
    });
}
4

2 回答 2

4

你应该使用SharedPreferences这个。

您有 5 个单选按钮,因此您可以使用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();

这应该足以实现您想要的行为

希望能帮助到你!

于 2014-12-15T06:42:30.533 回答
0

您可以将所选项目存储在 SharedPreferences 中。查看开发人员站点以获取有关 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");
于 2014-12-15T06:51:00.123 回答