如何使用片段添加时间选择器?

How to add timePicker using fragment?

我的应用程序中有 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.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)

您正在添加相同的 DialogFragment 实例两次,要解决此问题,请检查 TimePicker 是否添加了 getFragmentManager.findFragmentByTag("TimePicker")

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

 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 片段。当您需要来自这些编辑文本(您的 a、b、c、d... 变量)的值时,您可以在需要时阅读它,而不是在 onTimeSetMethod 中。

提供了 onStop 方法来处理 activity 娱乐案例。在这里我只是隐藏了活动的时间选择器。