【问题标题】:Is there a way to get the time of the TimePicker without using the TimePickerDialog?有没有办法在不使用 TimePickerDialog 的情况下获取 TimePicker 的时间?
【发布时间】:2013-04-23 04:05:02
【问题描述】:

我想知道是否有一种方法可以在不使用对话框的情况下获取用户在屏幕上的 TimePicker 中设置的时间。当我只能在屏幕上显示 TimePicker 时,我没有看到创建一个会弹出 TimePickerDialog 的按钮的意义。 IE。在本教程和许多其他类似的 TimePicker 教程中可以看到,拥有两个 TimePicker 是没有意义的:http://www.mkyong.com/android/android-time-picker-example/

我正在使用 picker.getCurrentHour() 和 picker.getCurrentMinute() 但它只是在我按下按钮后返回系统的当前时间。有没有办法获取用户输入的时间?

这是我在单击按钮后检索时间的方式:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_set_time);
    picker = new TimePicker(SetTimeActivity.this);
    Button save=(Button)findViewById(R.id.saveAlarmButton); 
    save.setOnClickListener(onSave);                
}

private View.OnClickListener onSave=new View.OnClickListener()
{
    public void onClick(View v)
    {
        hour=picker.getCurrentHour();
        min=picker.getCurrentMinute();

        System.out.println(hour + " : " + min);         
    }
};


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".SetTimeActivity" >

<Button
    android:id="@+id/saveAlarmButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/timePicker"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="28dp"
    android:text="Save" />

<TimePicker
    android:id="@+id/timePicker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="32dp" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="26dp"
    android:text="Set time for alarm"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

但它只是给了我当前时间。

【问题讨论】:

  • 我不知道为什么这不起作用,我会看一下 TimePickerDialog 源代码,看看它的工作方式是否不同,但只是看一眼,它看起来工作方式完全相同.
  • 您正在创建未添加到布局的选择器。这就是您获得当前时间的原因。
  • 对不起,我不明白。选择器就在那里……我正在看到并触摸它。从字面上看。
  • 添加布局xml,我可以帮你。你需要使用findViewById
  • 啊,我明白了。我在上面添加了xml

标签: java android time timepicker


【解决方案1】:

您的代码应该可以正常工作。当您尝试通过数字输入输入日期时,可能会出现问题。这是由于NumberPicker 更新,导致选择器失去焦点。所以你可以尝试确保选择器失去焦点,所以它会消耗当前的用户输入:

picker.clearFocus();
hour=picker.getCurrentHour();
min=picker.getCurrentMinute();

【讨论】:

    【解决方案2】:

    您可以像添加任何其他小部件一样将小部件添加到活动中。您可以像这样获取日期/时间:

        // Format date and time from pickers
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, datePicker.getYear());
        calendar.set(Calendar.MONTH, datePicker.getMonth());
        calendar.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());
        calendar.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
        calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute());
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
    

    【讨论】:

    • 正如问题中提到的,我的问题是它返回系统时间而不是选择的时间。
    【解决方案3】:

    picker classes 的定义是它打开一个对话框来选择一些东西:

    Android 为用户提供了选择时间或日期的控件 作为即用型对话框。

    当然,您可以随时尝试其他为您提供自定义选择器并获取其布局的解决方案,例如 this onehereherehere

    这样,您可以将选择器的布局放置在您想要的任何位置,而无需任何对话框。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-08
      • 1970-01-01
      • 1970-01-01
      • 2020-04-06
      • 2011-06-04
      • 2016-01-10
      • 2016-01-19
      相关资源
      最近更新 更多