Android 保存对话框的实例状态
我有以下单选按钮对话框,它按照我想要的方式工作,我也将尺寸 12 设置为默认值,但我现在需要做的是保存实例状态,即选择其他内容时我希望该尺寸为再次打开应用程序时将被选择。这是我的代码
final CharSequence[] items = {"12m", "16m", "20m"};
AlertDialog.Builder builder = new AlertDialog.Builder(Tweaks.this);
builder.setTitle("Select a size");
builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if(items[item] == "12m"){
Toast.makeText(this, "your size is 12", Toast.LENGTH_SHORT).show();
}
if(items[item] == "16m"){
Toast.makeText(this, "your size is 16", Toast.LENGTH_SHORT).show();
}
if(items[item] == "20m"){
Toast.makeText(this, "your size is 20", Toast.LENGTH_SHORT).show();
}
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
}).show();
谢谢您的帮助
I have the following radio button dialog which works the way i want i also have size 12 set as the default as well but what i need to now be able to do is save the instancestate that is when something else is selected i want that size to be selected when the app is opened again. Here is my code
final CharSequence[] items = {"12m", "16m", "20m"};
AlertDialog.Builder builder = new AlertDialog.Builder(Tweaks.this);
builder.setTitle("Select a size");
builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if(items[item] == "12m"){
Toast.makeText(this, "your size is 12", Toast.LENGTH_SHORT).show();
}
if(items[item] == "16m"){
Toast.makeText(this, "your size is 16", Toast.LENGTH_SHORT).show();
}
if(items[item] == "20m"){
Toast.makeText(this, "your size is 20", Toast.LENGTH_SHORT).show();
}
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
}).show();
Thank you for any help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如何保存当用户退出应用程序时 Android CheckBox 的状态?
但是请记住这是一个受控的保存状态。如果您的程序因缺乏资源而被终止,您应该在 onSaveInstanceState () 和 onRestoreInstanceState () 期间保存所有适当的信息
How to save the state of an Android CheckBox when the users exits the application?
However keep in mind this is a controlled save state. If your program should be killed due to lack of resources, you should save all appropriate info during onSaveInstanceState () and onRestoreInstanceState ()
使用 SharedPreferences 将最后选择的实例状态保存在偏好文件中,然后在代码中打开对话框时始终从偏好文件中读取状态,如果不存在偏好文件,则使用默认值。
Save the last selected instance state in perference file using SharedPreferences, then in your code always read the state from preference file when open the dialog, if no perference file existing, then use default.