gpt4 book ai didi

java - 如何使用 fragment 添加时间选择器?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:53:27 25 4
gpt4 key购买 nike

我的应用程序中有 4 个 timePicker。当用户双击 editText 时,timePicker 对话框会弹出。但是有时候我不小心点了两次以上,应用就崩溃了,说fragment already added。我怎样才能解决这个问题?与单击 editText 两次不同,我希望只需单击 editText 即可显示 timePicker 对话框。

public void onClick(View v) {
int id = v.getId();
if (id == R.id.editTextTI1) {
tp.setFlag(TimePick.FLAG_START_DATE);
FragmentTransaction ft = getFragmentManager().beginTransaction();
tp.show(ft, "TimePicker");
}
if (id == R.id.editTextTO1) {
tp.setFlag(TimePick.FLAG_END_DATE);
FragmentTransaction ft = getFragmentManager().beginTransaction();
tp.show(ft, "TimePicker");
}
if (id == R.id.editTextTI2) {
tp.setFlag(TimePick.FLAG_START_DATE1);
FragmentTransaction ft = getFragmentManager().beginTransaction();
tp.show(ft, "TimePicker");
}
if (id == R.id.editTextTO2) {
tp.setFlag(TimePick.FLAG_END_DATE1);
FragmentTransaction ft = getFragmentManager().beginTransaction();
tp.show(ft, "TimePicker");
}
if (id == R.id.editTextTI3) {
tp.setFlag(TimePick.FLAG_START_DATE2);
FragmentTransaction ft = getFragmentManager().beginTransaction();
tp.show(ft, "TimePicker");
}
if (id == R.id.editTextTO3) {
tp.setFlag(TimePick.FLAG_END_DATE2);
FragmentTransaction ft = getFragmentManager().beginTransaction();
tp.show(ft, "TimePicker");
}
if (id == R.id.editTextTI4) {
tp.setFlag(TimePick.FLAG_START_DATE3);
FragmentTransaction ft = getFragmentManager().beginTransaction();
tp.show(ft, "TimePicker");
}
if (id == R.id.editTextTO4) {
tp.setFlag(TimePick.FLAG_END_DATE3);
FragmentTransaction ft = getFragmentManager().beginTransaction();
tp.show(ft, "TimePicker");
}

}




public static class TimePick extends android.app.DialogFragment implements TimePickerDialog.OnTimeSetListener {

public static final int FLAG_START_DATE = 00;
public static final int FLAG_END_DATE = 01;
public static final int FLAG_START_DATE1 = 10;
public static final int FLAG_END_DATE1 = 11;
public static final int FLAG_START_DATE2 = 20;
public static final int FLAG_END_DATE2 = 21;
public static final int FLAG_START_DATE3 = 30;
public static final int FLAG_END_DATE3 = 31;
private int flag = 00;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);

return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity()));
}

public void setFlag(int i) {
flag = i;
}

@Override
public void onTimeSet(TimePicker view, int hourofDay, int minute) {

if (flag == FLAG_START_DATE) {
start.setText(Integer.toString(hourofDay) + ":" + Integer.toString(minute));
b = start.getText().toString();
}
if (flag == FLAG_END_DATE) {
end.setText(Integer.toString(hourofDay) + ":" + Integer.toString(minute));
c = end.getText().toString();
}
if (flag == FLAG_START_DATE1) {
start1.setText(Integer.toString(hourofDay) + ":" + Integer.toString(minute));
d = start1.getText().toString();
}
if (flag == FLAG_END_DATE1) {
end1.setText(Integer.toString(hourofDay) + ":" + Integer.toString(minute));
e1 = end1.getText().toString();
}
if (flag == FLAG_START_DATE2) {
start2.setText(Integer.toString(hourofDay) + ":" + Integer.toString(minute));
f = start2.getText().toString();
}
if (flag == FLAG_END_DATE2) {
end2.setText(Integer.toString(hourofDay) + ":" + Integer.toString(minute));
g = end2.getText().toString();
}
if (flag == FLAG_START_DATE3) {
start3.setText(Integer.toString(hourofDay) + ":" + Integer.toString(minute));
h = start3.getText().toString();
}
if (flag == FLAG_END_DATE3) {
end3.setText(Integer.toString(hourofDay) + ":" + Integer.toString(minute));
i = end3.getText().toString();
}

LogCat 错误

10-15 12:53:17.113    7943-7943/com.example.project.project E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.project.project, PID: 7943
java.lang.IllegalStateException: Fragment already added: TimePick{7c7eb96 #0 TimePicker}
at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1219)
at android.app.BackStackRecord.run(BackStackRecord.java:715)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:482)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)

最佳答案

您的代码硬编码且不易扩展,这可能会在未来导致许多错误。您应该尽可能地封装通用逻辑,因此在当前情况下:

 public void onClick(View v) {
EditText editText = (EditText) v;
if (tp==null || !tp.isAdded()){
FragmentTransaction ft = getFragmentManager().beginTransaction();
tp = new TimePick(editText);
tp.show(ft, "TimePicker");
}
}

@Override
public void onStop() {
super.onStop();
if (tp.isAdded()) tp.dismiss();
}

public static class TimePick extends android.app.DialogFragment implements TimePickerDialog.OnTimeSetListener {

private EditText editText;

public TimePick(EditText editText) {
this.editText = editText;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);

return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity()));
}

@Override
public void onTimeSet(TimePicker view, int hourofDay, int minute) {
editText.setText(Integer.toString(hourofDay) + ":" + Integer.toString(minute));
}
}

此代码不会尝试创建新的时间选择器(如果屏幕上有的话)。您可以将目标编辑文本传递给 timePicker fragment 。当您需要这些 edittexts(您的 a、b、c、d... 变量)的值时,您可以在需要时读取它,而不是在 onTimeSetMethod 中。

提供了 onStop 方法来处理 Activity 娱乐情况。在这里我只是隐藏了 Activity 的时间选择器。

关于java - 如何使用 fragment 添加时间选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33149101/

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