片段已添加:TimePick

Fragment already added: TimePick

我的应用程序中有两个 timepicker,一个用于 timeIn,另一个用于 timeOut。当 editText 被点击,它会显示 timePicker 对话框。

  private TimePick tp;
  tp = new TimePick(); 
  private static EditText timeIn;
  private static EditText timeOut;

 timeIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {   //shows timePick
                tp.setFlag(TimePick.FLAG_START_DATE);
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                tp.show(ft, "TimePicker");
            }
        });

        timeOut.setOnClickListener(new View.OnClickListener()
        {
            @Override
        public void onClick(View v)
            {

                tp.setFlag(TimePick.FLAG_END_DATE);
                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;
        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) {
                timeIn.setText(Integer.toString(hourofDay) + ":" + Integer.toString(minute));
                TimeIn = timeIn.getText().toString();
            }
            if (flag == FLAG_END_DATE) {
                timeOut.setText(Integer.toString(hourofDay) + ":" + Integer.toString(minute));
                TimeOut = timeOut.getText().toString();
            }
        }


    }

现在的问题是 timePicker 对话框仅在我双击 editText 时显示。是否可以将其更改为单击而不是双击?

如果我点击两次以上,应用程序会崩溃并显示 Fragment already added: TimePick{f1f4f2b #0 TimePicker 错误。我从之前的 中得到了一个解决方案,即添加一个 if-else 条件来检查对话框是否显示。但是 TimePicker 呢?它有类似 TimePicker d=tp.getTimepicker(); 的功能吗?

谢谢。

LogCat错误

11-27 07:17:40.617    2197-2197/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.project.myapplication, PID: 2197
    java.lang.IllegalStateException: Fragment already added: TimePick{f1f4f2b #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)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Is it possible to change it to one click instead of double click ?

是 xml 中的 EditText 将 focusableInTouchMode 属性设置为 true:

   <EditText
    android:focusableInTouchMode="false"
    ...
   />