gpt4 book ai didi

android - DatePickerFragment+EditText 实现与 AlertDialog

转载 作者:行者123 更新时间:2023-11-30 05:10:54 26 4
gpt4 key购买 nike

我正在制作提醒应用。我需要用 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 错误: fragment 应该是 静态的,这样它们可以被系统重新实例化,并且是匿名的 类不是静态的 顺便说一句,我在 3 年前的 android 开发类(class)中发现了这个版本,现在它不起作用(即使你创建静态类或为类创建单独的文件等)我尝试了很多事情

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) {
}

问题是:以前有人做过这样的东西吗?现在有机会重构这个东西并让它发挥作用吗?请帮忙!

最佳答案

获取您自己的 DatePickerFragment.java 和 MainActivity.java 类关于 DatePickerFragment.java不要OnDateSetListener接口(interface)

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

关于android - DatePickerFragment+EditText 实现与 AlertDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53770181/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com