时间选择器ontimeset是错误的

时间:2014-02-06 06:57:12

标签: java android time timepicker

我的应用程序中有一个日期和时间选择器,我刚才注意到时间错了。时间选择器显示的当前时间是i小时延迟,其上午/下午是错误的,例如当前时间为2:55 pm它将显示1:55 am这里的问题似乎是代码< / p>

更新:日期和时间选择

@Override
public void onClick(View v) {

    if (v == btnCalendar) {

        // Process to get Current Date
        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DATE)+1;

        // Launch Date Picker Dialog
        DatePickerDialog dpd = new DatePickerDialog(this,
                new DatePickerDialog.OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker view, int year,
                            int monthOfYear, int dayOfMonth) {
                        // Display Selected date in textbox
                        txtDate.setText(dayOfMonth + "-"
                                + (monthOfYear + 1) + "-" + year);

                    }
                }, mYear, mMonth, mDay);
        dpd.show();
    }
    if (v == btnTimePicker) {

        // Process to get Current Time
        final Calendar c = Calendar.getInstance();
        mHour = c.get(Calendar.HOUR)+1;
        mMinute = c.get(Calendar.MINUTE);

        // Launch Time Picker Dialog
        TimePickerDialog tpd = new TimePickerDialog(this,
                new TimePickerDialog.OnTimeSetListener() {

                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay,
                            int minute) {
                        // Display Selected time in textbox
                        txtTime.setText(hourOfDay + ":" + minute);
                    }
                }, mHour, mMinute, false);
        tpd.show();
    }
}
}

更新: 我所做的是在mHour = c.get(Calendar.HOUR_OF_DAY)+1;中加入+1,这个方法可能会有任何想法吗?

不固定是AM / PM按钮错误,它总是在开始时显示

更新:上午/下午不开始我完全错了,因为现在早上它显示下午但是在下午显示上午

更新: 在date中也存在一个问题。我做的是在mDay = c.get(Calendar.DATE)+1;中放置+1这个方法可能会有任何想法吗?

2 个答案:

答案 0 :(得分:2)

实际上,这是一个错误。有报道谷歌。你可以在这里找到它 http://code.google.com/p/android/issues/detail?id=24388http://code.google.com/p/android/issues/detail?id=18982

答案 1 :(得分:0)

您可以尝试这样的事情:

 <TimePicker
            android:id="@+id/timePicker"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/datePicker1"
            android:layout_centerHorizontal="true"
            android:layout_margin="20dp" />

TimePicker tp = (TimePicker)findViewById(R.id.timePicker);

 btn.setOnClickListener(new OnClickListener() 
 {

            @Override
            public void onClick(View v) 
            {
                // TODO Auto-generated method stub

                int Hour = tp.getCurrentHour();
                int Minute = tp.getCurrentMinute();

                    txtTime.setText(String.valueOf(Hour) + ":" + String.valueOf(Minute));

            }
        });