我正在制作提醒应用。我需要创建带有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) {
}
}
问题是:有人做过这样的事情吗?这里是否有机会重构该东西并使之工作?请帮助!
答案 0 :(得分:0)
获得您自己的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_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
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 类