在onTimeSet对话框后将TextView设置为HH:mm [已解决的问题]

时间:2015-03-01 20:44:10

标签: android timepicker android-timepicker

所以我试图找到获取TimePicker对话框输出的方法,将TextView设置为HH:mm AM / PM(例如:上午8:00)。我知道那里有东西,但我找不到它究竟合适的东西。我接受了让我接近的答案(从this question向Faraz Ahmed喊道:并加入了我自己的'旋转'。只是张贴希望它能帮助处于同样情况的人。

下面将获取onTimeSet中的输入并将其格式化为:HH:mm AM / PM。

inal Calendar myCalendar = Calendar.getInstance();
        String am_pm = "";
        myCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
        myCalendar.set(Calendar.MINUTE, minute);
        if (myCalendar.get(Calendar.AM_PM) == Calendar.AM)
            am_pm = "AM";
        else if (myCalendar.get(Calendar.AM_PM) == Calendar.PM)
            am_pm = "PM";
        String strHrsToShow = (myCalendar.get(Calendar.HOUR) == 0) ? "12" : myCalendar.get(Calendar.HOUR) + "";
        //UIHelper.showLongToastInCenter(context, strHrsToShow + ":" + myCalendar.get(Calendar.MINUTE) + " " + am_pm);

        if (minute == 0) {
            String mZeroMinute = "00";
            mSessionEndTimeTV.setText(strHrsToShow + ":" + mZeroMinute + " " + am_pm);
        } else {
            mSessionEndTimeTV.setText(strHrsToShow + ":" + myCalendar.get(Calendar.MINUTE) + " " + am_pm);

1 个答案:

答案 0 :(得分:0)

请参阅上文:

inal Calendar myCalendar = Calendar.getInstance();
        String am_pm = "";
        myCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
        myCalendar.set(Calendar.MINUTE, minute);
        if (myCalendar.get(Calendar.AM_PM) == Calendar.AM)
            am_pm = "AM";
        else if (myCalendar.get(Calendar.AM_PM) == Calendar.PM)
            am_pm = "PM";
        String strHrsToShow = (myCalendar.get(Calendar.HOUR) == 0) ? "12" : myCalendar.get(Calendar.HOUR) + "";
        //UIHelper.showLongToastInCenter(context, strHrsToShow + ":" + myCalendar.get(Calendar.MINUTE) + " " + am_pm);

        if (minute == 0) {
            String mZeroMinute = "00";
            mSessionEndTimeTV.setText(strHrsToShow + ":" + mZeroMinute + " " + am_pm);
        } else {
            mSessionEndTimeTV.setText(strHrsToShow + ":" + myCalendar.get(Calendar.MINUTE) + " " + am_pm);