解决dialog在横竖屏切换时消失
发表于:2024-11-26 作者:热门IT资讯网编辑
编辑最后更新 2024年11月26日,以AlertDialog为例一、将AlertDialog视图封装在dialogfragment(fragmentdeAlertDialog子类)实例中package com.example.t;imp
以AlertDialog为例
一、将AlertDialog视图封装在dialogfragment(fragmentdeAlertDialog子类)实例中
package com.example.t;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.widget.DatePicker;
public class DialogPickerFragment extends android.support.v4.app.DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
DatePicker dp = new DatePicker(getActivity());
return new AlertDialog.Builder(getActivity())
.setView(dp)
.setTitle("Dialog")
.setPositiveButton("OK", null)
.create();
}
}
二、在activity中调用
public void dialogpicker(View v) {
FragmentManager fm = getSupportFragmentManager();
DialogPickerFragment dialog = new DialogPickerFragment();
dialog.show(fm, "date");
}