【问题标题】:Set default time in timepicker based on devicetime根据设备时间在时间​​选择器中设置默认时间
【发布时间】:2013-12-18 06:33:00
【问题描述】:

我有一个 24 小时格式的时间选择器。现在,无论设备时间格式如何,我都想以 24 小时格式显示当前设备时间。现在我的设备是 12 小时格式,例如我的时间是下午 2:04,时间选择器应该是 14:04。

这是我的代码或时间选择器。它的布局不同,因为我在对话框中显示它们。

XML:

<LinearLayout android:id="@+id/datetimepickerLayout" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" xmlns:android="http://schemas.android.com/apk/res/android">
<DatePicker android:id="@+id/datePicker1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"></DatePicker>
<TimePicker android:id="@+id/timePicker1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"></TimePicker>

<Button
android:id="@+id/setdatetimeBtn1"
android:text="Set"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_gravity="center"/>

现在在我的 java 代码中:

timePicker1 = (TimePicker)dialog.findViewById(R.id.timePicker1);
datePicker1 = (DatePicker)dialog.findViewById(R.id.datePicker1);
setDateTimeBtn1 = (Button)dialog.findViewById(R.id.setdatetimeBtn1);
datePicker1.setCalendarViewShown(false);
timePicker1.setIs24HourView(true);

有什么想法吗?谢谢!

【问题讨论】:

标签: android datepicker timepicker


【解决方案1】:

AFAIK 默认使用设备当前时间,您可以使用以下代码进行设置。

Calendar c = Calendar.getInstance();
timePicker1.setCurrentHour(c.get(Calendar.HOUR));
timePicker1.setCurrentMinute(c.get(Calendar.MINUTE));

【讨论】:

  • 这正是我所需要的。谢谢!
  • 永远欢迎亲爱的!..如果它对你有帮助,请点赞接受。
  • 很抱歉。我前一段时间试图接受,但仍需要等待 3 分钟。完全忘记了。谢谢!
【解决方案2】:

您可以使用日历实例获取设备时间,如下所示:

Calendar c = Calendar.getInstance();

String CTime =c.get(Calendar.HOUR) + ":" + c.get(Calendar.MINUTE) +  ":" + c.get(Calendar.SECOND);

【讨论】:

    【解决方案3】:

    试试看。

     timePicker1 = (TimePicker)dialog.findViewById(R.id.timePicker1);
     datePicker1 = (DatePicker)dialog.findViewById(R.id.datePicker1);
     setDateTimeBtn1 = (Button)dialog.findViewById(R.id.setdatetimeBtn1);
        setDateTimeBtn1 .setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                String strDateTime = datePicker1 .getYear() + "-" + (datePicker1 .getMonth() + 1) + "-" + datePicker1 .getDayOfMonth() + " "
           + timePicker1 .getCurrentHour() + ":" + timePicker1.getCurrentMinute();
    
                Toast.makeText(TimeDate.this, "User selected " + strDateTime + "Time", Toast.LENGTH_LONG).show(); //Generate a toast only if you want 
                finish();   // If you want to continue on that TimeDateActivity
                 // If you want to go to new activity that code you can also write here
            }});
    

    【讨论】:

      【解决方案4】:

      @每个尝试过上述解决方案的人都没有工作,你必须使用你

      Calender.HOUR_OF_DAY
      

      而不是

      Calender.HOUR
      

      我今天整天在谷歌上搜索,直到我得到这个工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-04
        • 1970-01-01
        • 2023-04-08
        相关资源
        最近更新 更多