从 landscape/portrait 旋转时 DatePickerDialog 关闭
DatePickerDialog closes when rotating from landscape/portrait
我的 DatePickerDialog 宽度有问题,因为当我打开它并转动屏幕时 (landscape/portrait) 对话框停止。
现在我正在尝试将对话框的状态保存在 Bundle 中,但我真的不知道如何:/
代码:
public class ActivityNeki extends Activity {
private int datY, datM, datD;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
if(savedInstanceState == null){ setTheData(); writeTheData(); }
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt("izY", datY);
outState.putInt("izM", datM);
outState.putInt("izD", datD);
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
datY = savedInstanceState.getInt("izY");
datM = savedInstanceState.getInt("izM");
datD = savedInstanceState.getInt("izD");
writeTheData();
}
public void onClickOpenDPD(View view) { // the method that is caled from XML onClick
DatePickerDialog dpd = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override public void onDateSet(DatePicker view, int y, int m, int d) {
datY = y; datM = m; datD = d;
writeTheData();
}
}, datY, datM, datD);
dpd.show();
}
public void setTheData(){
Calendar c = Calendar.getInstance();
datY = c.get(Calendar.YEAR);
datM = c.get(Calendar.MONTH);
datD = c.get(Calendar.DAY_OF_MONTH);
}
public void writeTheData(){
// writes the data in a txtView
}
}
因此,如果有人知道如何执行此操作,我会为它定价!
提前致谢:)
可行的解决方案:(Big tnx to Taig)
public class ActivityNeki extends FragmentActivity {
/**
* the rest of the code
* is the same...
* I only edited this method:
*/
public void onClickOpenDPD(View view) { // the method that is caled from XML onClick
class MyDialogFragment extends DialogFragment {
@Override public void onDestroyView() {
if (getDialog() != null && getRetainInstance()) getDialog().setDismissMessage(null);
super.onDestroyView();
}
@Override public void onCreate(Bundle state) { super.onCreate(state);
setRetainInstance(true);
}
@Override public Dialog onCreateDialog(Bundle state) {
DatePickerDialog dpd = new DatePickerDialog( getActivity(), new DatePickerDialog.OnDateSetListener() {
@Override public void onDateSet(DatePicker view, int y, int m, int d) {
datY = y; datM = m; datD = d;
writeTheData();
} }, datY, datM, datD);
return dpd;
}
}
newDF = new MyDialogFragment();
newDF.show( getSupportFragmentManager(), null );
}
}
如果您愿意,我建议您使用 DialogFragments。
class MyDialogFragment extends DialogFragment {
@Override
public void onCreate( Bundle state ) {
super.onCreate( state );
setRetainInstance( true );
}
@Override
public Dialog onCreateDialog( Bundle state ) {
return new DatePickerDialog( ... );
}
}
在您的 activity 中,您可以通过调用 new MyDialogFragment().show( getSupportFragmentManager(), null )
来启动对话框。对话框将重新打开,您不必自己坚持当前方向更改的选择!
我的 DatePickerDialog 宽度有问题,因为当我打开它并转动屏幕时 (landscape/portrait) 对话框停止。
现在我正在尝试将对话框的状态保存在 Bundle 中,但我真的不知道如何:/
代码:
public class ActivityNeki extends Activity {
private int datY, datM, datD;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
if(savedInstanceState == null){ setTheData(); writeTheData(); }
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt("izY", datY);
outState.putInt("izM", datM);
outState.putInt("izD", datD);
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
datY = savedInstanceState.getInt("izY");
datM = savedInstanceState.getInt("izM");
datD = savedInstanceState.getInt("izD");
writeTheData();
}
public void onClickOpenDPD(View view) { // the method that is caled from XML onClick
DatePickerDialog dpd = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override public void onDateSet(DatePicker view, int y, int m, int d) {
datY = y; datM = m; datD = d;
writeTheData();
}
}, datY, datM, datD);
dpd.show();
}
public void setTheData(){
Calendar c = Calendar.getInstance();
datY = c.get(Calendar.YEAR);
datM = c.get(Calendar.MONTH);
datD = c.get(Calendar.DAY_OF_MONTH);
}
public void writeTheData(){
// writes the data in a txtView
}
}
因此,如果有人知道如何执行此操作,我会为它定价!
提前致谢:)
可行的解决方案:(Big tnx to Taig)
public class ActivityNeki extends FragmentActivity {
/**
* the rest of the code
* is the same...
* I only edited this method:
*/
public void onClickOpenDPD(View view) { // the method that is caled from XML onClick
class MyDialogFragment extends DialogFragment {
@Override public void onDestroyView() {
if (getDialog() != null && getRetainInstance()) getDialog().setDismissMessage(null);
super.onDestroyView();
}
@Override public void onCreate(Bundle state) { super.onCreate(state);
setRetainInstance(true);
}
@Override public Dialog onCreateDialog(Bundle state) {
DatePickerDialog dpd = new DatePickerDialog( getActivity(), new DatePickerDialog.OnDateSetListener() {
@Override public void onDateSet(DatePicker view, int y, int m, int d) {
datY = y; datM = m; datD = d;
writeTheData();
} }, datY, datM, datD);
return dpd;
}
}
newDF = new MyDialogFragment();
newDF.show( getSupportFragmentManager(), null );
}
}
如果您愿意,我建议您使用 DialogFragments。
class MyDialogFragment extends DialogFragment {
@Override
public void onCreate( Bundle state ) {
super.onCreate( state );
setRetainInstance( true );
}
@Override
public Dialog onCreateDialog( Bundle state ) {
return new DatePickerDialog( ... );
}
}
在您的 activity 中,您可以通过调用 new MyDialogFragment().show( getSupportFragmentManager(), null )
来启动对话框。对话框将重新打开,您不必自己坚持当前方向更改的选择!