我创建了一个对话框片段正在建设和回归警报对话框 from 创建对话框时方法。 AlertDialog 包含两个 EditText 视图。
我正在设置这两个编辑文本的初始值创建对话框时该方法非常有效,直到我旋转手机并且所有更改都会丢失/恢复为初始值,因为创建对话框时被召回。
所以我的问题是我应该在哪里放置初始值,以便它们仅在您第一次打开对话框时设置,如果您完成更改并旋转手机,我保留并重新连接的最后状态?
下面我粘贴了我的代码的简化版本。一种解决方案可能是在 newInstance() 方法中初始化类属性,但随后我需要将它们设为静态。其他解决方案可能是通过 Bundle 传递值,但没有 put-methods 将 Calendar 作为参数类型。
什么是最佳实践?
public class MyDialogFragment extends DialogFragment implements OnClickListener, OnDateSetListener, OnQuantitySetListener
{
private EditText editText1, editText2
private MyObject myObject;
public static MyDialogFragment newInstance()
{
return new MyDialogFragment ();
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater factory = LayoutInflater.from(getActivity());
final View v = factory.inflate(R.layout.my_layout, null);
editText1 = (EditText) v.findViewById(R.id.text1);
editText2 = (EditText) v.findViewById(R.id.text2);
myObject = <get the object from database>;
editText1.setText(myObject.attribute1);
editText2.setText(myObject.attribute2);
bindDataToViews();
return new AlertDialog.Builder(getActivity())
.setIconAttribute(R.drawable.add)
.setTitle("Title of the dialog")
.setView(v)).create();
}
... other methods using getting the values from EditText and putting them back to MyObject
}
Calendar
is Serializable
所以你可以把它放在Bundle
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)