用于对话的Android保存实例状态

时间:2011-05-06 22:15:36

标签: android dialog radio-button save

我有以下单选按钮对话框,按照我想要的方式工作,我也将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();

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

How to save the state of an Android CheckBox when the users exits the application?

但请记住,这是一个受控制的保存状态。如果您的程序因资源不足而被杀死,您应该在onSaveInstanceState () and onRestoreInstanceState ()

期间保存所有适当的信息

答案 1 :(得分:0)

使用SharedPreferences将最后选定的实例状态保存在perference文件中,然后在代码中始终在打开对话框时从首选项文件中读取状态,如果没有存在perference文件,则使用default。