如何在android中保存对话框的状态?我有以下带有单选按钮的对话框,但无法弄清楚如何保存对话框的状态。谢谢你的帮助
final CharSequence[] items = {"Item 1", "Item 2", "Item 3"};
AlertDialog.Builder builder = new AlertDialog.Builder(Tweaks.this);
builder.setTitle("Pick an item");
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
}).show();
答案 0 :(得分:1)
您应该在用户点击时存储所选项目的位置。然后在显示列表时查找以前存储的索引。如果之前没有存储的值,则返回-1。
我有一个应用偏好帮助类......
public class AppPreferences {
private static final String APP_SHARED_PREFS = "myApp_preferences"; // Name of the file -.xml
private SharedPreferences appSharedPrefs;
private Editor prefsEditor;
public AppPreferences(Context context)
{
this.appSharedPrefs = context.getSharedPreferences(APP_SHARED_PREFS, Activity.MODE_PRIVATE);
this.prefsEditor = appSharedPrefs.edit();
}
public int getItemIndex() {
return appSharedPrefs.getInt("itemIndex", -1);
}
public void saveItemIndex(int i) {
prefsEditor.putInt("itemIndex", i);
prefsEditor.commit();
}
}
然后,在我的代码中,我创建了一个字段变量......
protected AppPreferences appPrefs;
在Activity onCreate()...
中实例化它的实例appPrefs = new AppPreferences(getApplicationContext());
然后用......替换你的“-1”
builder.setSingleChoiceItems(items, appPrefs.getItemIndex(), new DialogInterface.OnClickListener() {
在你的onClick()中确保你......
appPrefs.saveItemIndex(item);
答案 1 :(得分:0)
我将状态保存在DialogFragment中的成员变量中。以下代码在关闭对话框时保存状态,但在应用程序关闭时保存不。
public class MyDialogFragment extends DialogFragment {
//this is the default value, set when the dialog is created
private String myValue = "any initial String";
private TextEdit myTextEdit;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
//construct the dialog
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
ViewGroup myView = (ViewGroup)inflater.inflate(R.layout.my_view, null);
builder.setView(myView);
//find the view and set the value
myTextEdit = (TextView)myView.findViewById(R.id.my_text_edit);
myTextEdit.setText(myValue);
return builder.create();
}
@Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
//when the dialog is dismissed, save the users input and overwrite the initial value
myValue = myTextEdit.getText();
}
}
答案 2 :(得分:0)
您可以使用首选项,但我建议使用新的androidx设计模式。基本上,关闭对话框状态不应在应用程序关闭时保存。那就是我们想要的(因此我们不需要首选项或类似的东西)。
您可以使用android视图模型和保存的状态来完成这项工作。
这里是显示要与gradle(https://developer.android.com/jetpack/androidx/releases/lifecycle)一起使用的依赖项和版本的链接
implementation 'androidx.lifecycle:lifecycle-viewmodel-savedstate:1.0.0-rc01'
您可以这样做。
public class HelloAndroidViewModel extends AndroidViewModel{
private SavedStateHandle mState;
public HelloAndroidViewModel(SavedStateHandle savedStateHandle) {
mState = savedStateHandle;
}
public void saveParam(int param){
mState.set("key", param);
}
public int getParam(){
Integer param = mState.get("key");
if(param != null){
return param.intValue();
}
// default value
else {
return -1;
}
}
}
public class HelloAndroid extends Activity {
HelloAndroidViewModel viewModel;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
viewModel = ViewModelProviders.of(this, new SavedStateViewModelFactory(getApplication(), this).get(AndroidViewModel.class);
}
然后,您可以使用值来检索和存储对话框。
final CharSequence[] items = {"Item 1", "Item 2", "Item 3"};
AlertDialog.Builder builder = new AlertDialog.Builder(Tweaks.this);
builder.setTitle("Pick an item");
builder.setSingleChoiceItems(items, viewModel.getParam(), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
// save ui state
viewModel.setParam(item);
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
}).show();