使用 AlertDialog 实现 DatePickerFragment+EditText
Posted
技术标签:
【中文标题】使用 AlertDialog 实现 DatePickerFragment+EditText【英文标题】:DatePickerFragment+EditText implementation with AlertDialog 【发布时间】:2019-05-15 03:52:11 【问题描述】:我正在制作提醒应用。我需要用 3 个 EditText(任务的标题、日期、时间)创建 ALertDialog。它不起作用,因为此代码自 2016 年底以来已过时
//Main dialog class
public class AddingTaskDialogFragment extends DialogFragment
private AddingTaskListener addingTaskListener;
public interface AddingTaskListener
void onTaskAdded(ModelTask newTask);
void onTaskAddingCancel();
@Override
public void onAttach(Activity activity)
super.onAttach(activity);
try
addingTaskListener = (AddingTaskListener) activity;
catch (ClassCastException e)
throw new ClassCastException(activity.toString() + " must implement AddingTaskListener");
//Main Method
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.dialog_title);
LayoutInflater inflater = getActivity().getLayoutInflater();
View container = inflater.inflate(R.layout.dialog_task, null);
//Creating 3 TextEdits (TItle, date, time)
final TextInputLayout tilTitle = (TextInputLayout) container.findViewById(R.id.tilDialogTaskTitle);
final EditText etTitle = tilTitle.getEditText();
TextInputLayout tilDate = (TextInputLayout) container.findViewById(R.id.tilDialogTaskDate);
final EditText etDate = tilDate.getEditText();
final TextInputLayout tilTime = (TextInputLayout) container.findViewById(R.id.tilDialogTaskTime);
final EditText etTime = tilTime.getEditText();
tilTitle.setHint(getResources().getString(R.string.task_title));
tilDate.setHint(getResources().getString(R.string.task_date));
tilTime.setHint(getResources().getString(R.string.task_time));
builder.setView(container);
final ModelTask task=new ModelTask();
final Calendar calendar=Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY)+1);
//The most interesting part
etDate.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
if (etDate.length() == 0)
etDate.setText(" ");
DatePickerFragment 错误:片段应该是 静态的,这样它们可以被系统重新实例化,并且是匿名的 类不是静态的 顺便说一句,我在 3 岁的 android 开发课程中发现了这个版本,现在它不起作用(即使你创建静态类或为类制作单独的文件等)我尝试了很多事情来做
DialogFragment datePickerFragment = new DatePickerFragment()
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, monthOfYear);
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
etDate.setText(Utils.getDate(calendar.getTimeInMillis()));
@Override
public void onCancel(DialogInterface dialog)
etDate.setText(null);
;
datePickerFragment.show(getFragmentManager(), "DatePickerFragment");
);
//the same thing with TImePickerFragment
etTime.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
if (etTime.length() == 0)
etTime.setText(" ");
DialogFragment timePickerFragment = new TimePickerFragment()
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute)
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
etTime.setText(Utils.getTime(calendar.getTimeInMillis()));
@Override
public void onCancel(DialogInterface dialog)
etTime.setText(null);
;
timePickerFragment.show(getFragmentManager(), "TimePickerFragment");
);
//OK button saves EditTexts content
builder.setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
task.setTitle(etTitle.getText().toString());
if (etDate.length() != 0 || etTime.length() != 0)
task.setDate(calendar.getTimeInMillis());
addingTaskListener.onTaskAdded(task);
dialog.dismiss();
);
builder.setNegativeButton(R.string.dialog_cancel, new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
addingTaskListener.onTaskAddingCancel();
dialog.cancel();
);
// AlertDialog
AlertDialog alertDialog = builder.create();
alertDialog.setOnShowListener(new DialogInterface.OnShowListener()
@Override
public void onShow(DialogInterface dialog)
final Button positiveButton = ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_POSITIVE);
if (etTitle.length() == 0)
positiveButton.setEnabled(false);
tilTitle.setError(getResources().getString(R.string.dialog_error_empty_title));
etTitle.addTextChangedListener(new TextWatcher()
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
if (s.length() == 0)
positiveButton.setEnabled(false);
tilTitle.setError(getResources().getString(R.string.dialog_error_empty_title));
else
positiveButton.setEnabled(true);
tilTitle.setErrorEnabled(false);
@Override
public void afterTextChanged(Editable s)
);
);
return alertDialog;
DatePickerFragment 代码:
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener
public DatePickerFragment()
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(),(DatePickerDialog.OnDateSetListener) getActivity(), year, month, day);
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
问题是:以前有没有人做过这样的事情?这里有机会重构这个东西并让它工作吗?请帮忙!
【问题讨论】:
【参考方案1】:让你自己DatePickerFragment.java and MainActivity.java
课程
在 DatePickerFragment.java 上
不要 OnDateSetListener 接口
public class DatePickerFragment extends DialogFragment
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
/**Init Calendar**/
final Calendar c = Calendar.getInstance();
/**Get Year**/
int year = c.get(Calendar.YEAR);
/**Get Month**/
int month = c.get(Calendar.MONTH);
/**Get Date of Month**/
int day = c.get(Calendar.DAY_OF_MONTH);
/**Return a DatePickerDialog**/
return new DatePickerDialog(getActivity(), dateSetListener, year, month, day);
/**Date Set Listener**/
private DatePickerDialog.OnDateSetListener dateSetListener =
new DatePickerDialog.OnDateSetListener()
public void onDateSet(DatePicker view, int year, int month, int day)
/**Show Toast when Select any Date**/
Toast.makeText(getActivity(), "Selected date is " + view.getYear() + " / " + (view.getMonth() + 1) + " / " + view.getDayOfMonth(), Toast.LENGTH_SHORT).show();
;
然后进入**XML **
<!--Button to Show a Date Picker Fragment (Dialog)-->
<Button
android:id="@+id/btnDatePicker"
android:layout_
android:layout_
android:layout_gravity="center"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="20dp"
android:onClick="showFragment"
android:text="Show DialogFragment" />
<TextView
android:layout_
android:layout_
android:layout_gravity="center"
android:padding="10dp"
android:layout_marginTop="20dp"
android:text="DatePicker Widget"
android:textColor="@android:color/holo_red_dark"
android:textSize="18sp"
android:textStyle="bold" />
然后进入我们的 MainActivity
public void showFragment(View view)
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "date picker");
如果这工作正常...然后扩展 DatePickerDialog 类
【讨论】:
我需要在内部使用 DatePickerFragment:etDate.OnClickListener(而不是你提供的按钮)。所有这些都在 OnCreateDialog 方法中,即我在另一个 DialogFragment (AddingTaskDialogFragment) 中使用 DatePickerFragment。在 MainActivity 中,我称它为AddingTaskDialogFragment。以上是关于使用 AlertDialog 实现 DatePickerFragment+EditText的主要内容,如果未能解决你的问题,请参考以下文章
Android实现点击AlertDialog上按钮时不关闭对话框
Android中使用AlertDialog实现几种不同的对话框