如果我想从自定义对话框中打开日期选择器对话框,请问有人可以帮我吗,我有一个如下所示的自定义对话框类
public class ReceiptsDialog extends CustomActivity {
private static ReceiptsDialog instance;
private static Activity mContext;
private static Dialog customDialog;
static EditText tv_date;
public static ReceiptsDialog getInstance() {
if (instance == null) {
instance = new ReceiptsDialog();
}
return instance;
}
public void createDialog(Activity mContext,Dialogclick dialogclick) {
this.mContext = mContext;
this.dialogclick = dialogclick;
customDialog = new Dialog(mContext);
customDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
customDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
customDialog.setContentView(R.layout.receipts_dialog);
customDialog.setCanceledOnTouchOutside(false);
customDialog.show();
init();
}
private void init() {
tv_date = customDialog.findViewById(R.id.et_date);
setTouchNClick(tv_date);
}
@Override
public void onClick(View v) {
super.onClick(v);
if (v == tv_date) {
DialogFragment dFragment = new DatePickerFragment();
dFragment.show(getSupportFragmentManager(), "Date Picker");
}
}
@SuppressLint("ValidFragment")
public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dpd = new DatePickerDialog(getActivity(), R.style.MyCalTheme, this, year, month, day);
return dpd;
}
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
final SimpleDateFormat dateViewFormatter = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);//hh:mm:ss
Calendar choosen = Calendar.getInstance();
choosen.set(year, month, dayOfMonth);
tv_date.setText(dateViewFormatter.format(choosen.getTime()));
}
}
}
我想在点击tv_date时打开日期选择器对话框
但这给出了错误:
W / System.err:java.lang.IllegalStateException: 活动已在处被破坏 android.support.v4.app.FragmentManagerImpl。 enqueueAction(FragmentManager.java:2114)位于android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:683) android.support.v4.app.BackStackRecord。
提交(BackStackRecord.java:637) 在android.support.v4.app.DialogFragment.show(DialogFragment.java:144) 在com.omegasoftware.olivepos.home.functions.ReceiptsDialog.onClick(ReceiptsDialog.java:141) 在android.view.View.performClick(View.java:5205) 在android.view.View $ PerformClick.run(View.java:21164) 在android.os.Handler.handleCallback(Handler.java:739) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:5417) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)
答案 0 :(得分:0)
工作解决方案 常规对话框打开日期选择器:使用以下示例
public class ReceiptsDialog extends CustomActivity {
private Activity mContext;
private Dialog customDialog;
EditText tv_date;
public void createDialog(Activity mContext) {
this.mContext = mContext;
customDialog = new Dialog(mContext);
customDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
customDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
customDialog.setContentView(R.layout.receipts_dialog);
customDialog.setCanceledOnTouchOutside(false);
customDialog.show();
init();
}
private void init() {
tv_date = customDialog.findViewById(R.id.et_date);
setTouchNClick(tv_date);
}
@Override
public void onClick(View v) {
super.onClick(v);
if (v == tv_date) {
View vv = v;
Calendar c = Calendar.getInstance();
DatePickerDialog dpd = new DatePickerDialog(mContext,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
String s = dayOfMonth + "-"
+ (monthOfYear + 1) + "-" + year;
tv_date.setText(dayOfMonth + "-"
+ (monthOfYear + 1) + "-" + year);
}
}, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE));
dpd.show();
}
}
}