TimePicker - 如何获得AM或PM?

时间:2011-09-23 09:49:51

标签: android timepicker

我想根据用户在TimePicker中的选择设置闹钟。 TimePicker设置为AM / PM模式。为了知道用户是否希望将其闹钟设置为上午10点或下午10点,我应该如何获得AM / PM值?

听众TimePickerDialog.OnTimeSetListener只通过了几小时和几分钟。

9 个答案:

答案 0 :(得分:20)

以上答案是对的,我找到了找到AM PM的最简单方法。

TimePickerDialog.OnTimeSetListener onStartTimeListener = new OnTimeSetListener() {

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        String AM_PM ;
        if(hourOfDay < 12) {
            AM_PM = "AM";
        } else {
            AM_PM = "PM";
        }

        mStartTime.setText(hourOfDay + " : " + minute + " " + AM_PM );
    }
};

答案 1 :(得分:16)

来自

的回调
public void onTimeSet(TimePicker view, int hourOfDay, int minute) 

调用以下功能

private String getTime(int hr,int min) {
        Time tme = new Time(hr,min,0);//seconds by default set to zero
        Format formatter;
        formatter = new SimpleDateFormat("h:mm a");
        return formatter.format(tme);
    }

这些函数会在下午1:12返回

答案 2 :(得分:11)

您可以使用以下方法从Timepicker获取AM / PM。 Timepicker是一个viewgroup。这就是为什么我们可以得到它的子视图索引2,它表明AM / PM实际上是一个按钮。所以我们可以得到它的文本。

 Mycallback=new OnTimeSetListener() {

        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        ViewGroup vg=(ViewGroup) view.getChildAt(0);
            Toast.makeText(TimePickerDemoActivity.this, hourOfDay+":"+minute+":"+((Button)vg.getChildAt(2)).getText().toString(), 5000).show();

        }
    };

答案 3 :(得分:7)

我希望将此作为评论,但我没有足够的声誉。到目前为止,最简洁的获取AM或PM的方法是使用三元运算符:

String am_pm = (hourOfDay < 12) ? "AM" : "PM";

答案 4 :(得分:3)

这里我从系统中获取当前日期和时间,并使用日期和时间选择器对话框进行更新

private int  mMonth,mYear,mDay,mHour,mMin;

public static final int DTPKR = 1;

public static final int TMPKR = 2;

// Getting  the current date and time into DatePicker dialog

    public void getCurrentDate(){
       final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);
    } 

    public void getCurrentTime(){
                final Calendar c = Calendar.getInstance();
            mHour = c.get(Calendar.HOUR_OF_DAY);
            mMin = c.get(Calendar.MINUTE);

      } 

现在创建对话框并更新值 //创建对话框

    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DTPKR:
            return new DatePickerDialog(this,lisDate, mYear, mMonth, mDay);
        case TMPKR:
            return new TimePickerDialog(this,lisTime,mHour, mMin, false);
        }
        return null;
    }

    //setting date and updating editText 

       DatePickerDialog.OnDateSetListener lisDate = new DatePickerDialog.OnDateSetListener() {
               @Override
               public void onDateSet(DatePicker view, int year, int monthOfYear,
                 int dayOfMonth) {
                   mYear = year;
                    mMonth = monthOfYear;
                    mDay = dayOfMonth;
                etDate.setText(new StringBuilder() .append(mDay).append("/").append(mMonth+1).append("/").append(mYear));
                getCurrentDate();     

             }
    };

  //setting time and updating editText 

    TimePickerDialog.OnTimeSetListener lisTime=new TimePickerDialog.OnTimeSetListener() {

        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            mHour=hourOfDay;
            mMin=minute;

             String AM_PM ;
                if(hourOfDay < 12) {
                    AM_PM = "AM";

                } else {
                    AM_PM = "PM";
                    mHour=mHour-12;
                }
                etTime.setText(mHour+":"+mMin+" "+AM_PM);
                getCurrentDate();
        }
    };

答案 5 :(得分:2)

不确定它是否会有所帮助,但我在做同样的事情。请看我的回复 TimePickerDialog and AM or PM

答案 6 :(得分:2)

我是使用日历做的,这应该适用于所有版本:

public void onTimeSet(TimePicker view, int hourOfDay, int minute){
    String am_pm = "";

    Calendar datetime = Calendar.getInstance();
    datetime.set(Calendar.HOUR_OF_DAY, hourOfDay);
    datetime.set(Calendar.MINUTE, minute);

    if (datetime.get(Calendar.AM_PM) == Calendar.AM)
        am_pm = "AM";
    else if (datetime.get(Calendar.AM_PM) == Calendar.PM)
        am_pm = "PM";

    String strHrsToShow = (datetime.get(Calendar.HOUR) == 0) ?"12":Integer.toString( datetime.get(Calendar.HOUR) );


    ((Button)getActivity().findViewById(R.id.btnEventStartTime)).setText( strHrsToShow+":"+datetime.get(Calendar.MINUTE)+" "+am_pm );
}

请注意Calendar.HOUR_OF_DAYCalendar.HOUR的使用情况,如果您查看了解这两个技巧的文档。

答案 7 :(得分:1)

我认为回调将以24格式小时调用,因为。 Dialog在内部使用TimePickerTimePickergetCurrentHour moethod,以0-23格式返回小时。

答案 8 :(得分:0)

例如:

TimePicker sellerTime = (TimePicker) findViewById(R.id.sellerTime);

Integer.toString(((sellerTime.getCurrentHour().intValue()) > 12) ?  ((sellerTime.getCurrentHour().intValue()) - 12) : 
(sellerTime.getCurrentHour().intValue())