gpt4 book ai didi

android - 如何保存以前选择的单选按钮状态,并在应用程序退出和启动时始终处于第一个选择状态?

转载 作者:搜寻专家 更新时间:2023-11-01 07:56:05 26 4
gpt4 key购买 nike

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

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

最佳答案

您应该为此使用 SharedPreferences

您有 5 个单选按钮,因此您可以使用具有默认值(例如 0)的 SharedPreferences,以防您之前没有使用过该对话框。

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

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

希望对您有所帮助!

关于android - 如何保存以前选择的单选按钮状态,并在应用程序退出和启动时始终处于第一个选择状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27478571/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com