Android 使用时间选择器设置时间

Android 使用时间选择器设置时间,android,timepicker,Android,Timepicker,有两个编辑文本: edittext时间选择器的一次单击应弹出 如何使用时间选择器选择日期并在两个编辑文本中设置日期 我在谷歌上搜索了一下计时器 但是我不知道如何在单击edittext和 设定时间 XML: <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" androi

有两个编辑文本

edittext时间选择器的一次单击应弹出

如何使用时间选择器选择日期并在两个编辑文本中设置日期
  • 我在谷歌上搜索了一下计时器
  • 但是我不知道如何在单击edittext和 设定时间
XML

<LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:padding="3dp" >

                <TextView
                    android:id="@+id/lunch_from_textview_id"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/image1"
                    android:layout_toRightOf="@+id/lunch_button_id"
                    android:gravity="center"
                    android:paddingLeft="30dp"
                    android:text="From"
                    android:textColor="#000000"
                    android:textSize="15sp" />

                <EditText
                    android:id="@+id/from_lunch_edit_text_id"
                    android:layout_width="55dp"
                    android:layout_height="40dp" />

                <TextView
                    android:id="@+id/lunch_to_textview_id"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBottom="@+id/lunch_button_id"
                    android:layout_toRightOf="@+id/textView1"
                    android:gravity="center"
                    android:text="To"
                    android:textColor="#000000"
                    android:textSize="15sp" />

                <requestFocus />

                <EditText
                    android:id="@+id/to_lunch_edit_text_id"
                    android:layout_width="55dp"
                    android:layout_height="40dp"
                    android:ems="10" />
            </LinearLayout>

对于打开时间Pcker对话框,您必须放置一个按钮,或者您应该使用一个EditText,以便您可以在按钮或EditText的单击事件中写入


您可以找到示例。

我们可以在smriti3上完成,请尝试我下面的示例代码,如果您发现任何问题,请告诉我

 <EditText 
    android:id="@+id/add_TimePicker"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:singleLine="true"
    android:inputType="none"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:textColor="#38251f"                   
    android:focusable="false"/>    

单击编辑文本或按钮

mPickTimeButton.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            // show the time picker dialog
            DialogFragment newFragment = new TimePickerFragment();
            newFragment.show(getSupportFragmentManager(), "timePicker");
        }
    }); // this is on onCreate() method..

@Override
public void onTimePicked(Calendar time)
{
    // display the selected time in the TextView
    mPickedTimeText.setText(DateFormat.format("h:mm a", time));
}
然后创建一个类TimePickerFragment,该类通过DialogFragment

  public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener
{
  private TimePickedListener mListener;

  @Override
 public Dialog onCreateDialog(Bundle savedInstanceState)
  {
    // use the current time as the default values for the picker
    final Calendar c = Calendar.getInstance();
    int hour = c.get(Calendar.HOUR_OF_DAY);
    int minute = c.get(Calendar.MINUTE);

    // create a new instance of TimePickerDialog and return it
    return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity()));
  }

  @Override
   public void onAttach(Activity activity)
  {
    // when the fragment is initially shown (i.e. attached to the activity), cast the activity to the callback interface type
    super.onAttach(activity);
    try
    {
        mListener = (TimePickedListener) activity;
    }
    catch (ClassCastException e)
    {
        throw new ClassCastException(activity.toString() + " must implement " + TimePickedListener.class.getName());
    }
  }

 @Override
 public void onTimeSet(TimePicker view, int hourOfDay, int minute)
 {
    // when the time is selected, send it to the activity via its callback interface method
    Calendar c = Calendar.getInstance();
    c.set(Calendar.HOUR_OF_DAY, hourOfDay);
    c.set(Calendar.MINUTE, minute);

    mListener.onTimePicked(c);
 }

  public static interface TimePickedListener
 {
    public void onTimePicked(Calendar time);
 }
}

您希望在哪个编辑文本中为编辑文本和设置时间。。。。分别:)好的…所以你必须为它调用click event方法…并且刚刚声明了另一个静态的final int TIME_DIALOG_ID_MY=1变量..@smriti3但是如果你运行你的应用程序,它将崩溃,因为你在oncreate上有调用片段..那么你的当前时间选择器视图将如何显示呢?哦…是的,对。。。。让我在fragment类中重新创建整个过程。。。。。是的,我想这就是问题所在。。。。谢谢你的帮助。。。我会将答案标记为正确:)@smriti3谢谢你的回答……是的,如果你有问题,请和房间里的人聊天……谢谢。@PiyushGupta。您是否应该不使用
对话框片段
here@Raghunandan是的……为什么不呢??
mPickTimeButton.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            // show the time picker dialog
            DialogFragment newFragment = new TimePickerFragment();
            newFragment.show(getSupportFragmentManager(), "timePicker");
        }
    }); // this is on onCreate() method..

@Override
public void onTimePicked(Calendar time)
{
    // display the selected time in the TextView
    mPickedTimeText.setText(DateFormat.format("h:mm a", time));
}
  public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener
{
  private TimePickedListener mListener;

  @Override
 public Dialog onCreateDialog(Bundle savedInstanceState)
  {
    // use the current time as the default values for the picker
    final Calendar c = Calendar.getInstance();
    int hour = c.get(Calendar.HOUR_OF_DAY);
    int minute = c.get(Calendar.MINUTE);

    // create a new instance of TimePickerDialog and return it
    return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity()));
  }

  @Override
   public void onAttach(Activity activity)
  {
    // when the fragment is initially shown (i.e. attached to the activity), cast the activity to the callback interface type
    super.onAttach(activity);
    try
    {
        mListener = (TimePickedListener) activity;
    }
    catch (ClassCastException e)
    {
        throw new ClassCastException(activity.toString() + " must implement " + TimePickedListener.class.getName());
    }
  }

 @Override
 public void onTimeSet(TimePicker view, int hourOfDay, int minute)
 {
    // when the time is selected, send it to the activity via its callback interface method
    Calendar c = Calendar.getInstance();
    c.set(Calendar.HOUR_OF_DAY, hourOfDay);
    c.set(Calendar.MINUTE, minute);

    mListener.onTimePicked(c);
 }

  public static interface TimePickedListener
 {
    public void onTimePicked(Calendar time);
 }
}