TimePicker - 如何获取上午或下午?

发布于 2024-12-06 01:23:22 字数 178 浏览 2 评论 0原文

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

侦听器 TimePickerDialog.OnTimeSetListener 仅传递小时和分钟。

I want to set an alarm based on a user's selection in TimePicker. TimePicker is set to AM/PM mode. In order to know if a user wants his alarm to set to 10 AM or 10 PM, how should I get the AM/PM value?

The listener TimePickerDialog.OnTimeSetListener passes only hours and minutes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

寂寞美少年 2024-12-13 01:23:22

上面的答案是正确的,我找到了找到 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 );
    }
};

Above answers are right i found simplest way to find 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 );
    }
};
幻想少年梦 2024-12-13 01:23:22

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 PM

On callback from

public void onTimeSet(TimePicker view, int hourOfDay, int minute) 

call below function

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);
    }

The functions return something like this 1:12 PM

遮云壑 2024-12-13 01:23:22

您可以使用以下方法从 Timepicker 获取 AM/PM。
Timepicker 是一个视图组。这就是为什么我们可以获得它的子视图索引 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();

        }
    };

You can get AM/PM from Timepicker using following method.
Timepicker is a viewgroup.That's why we can get its child view index 2 which demonstrates AM/PM is actually a button. So we can get its text.

 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();

        }
    };
明月松间行 2024-12-13 01:23:22

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

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

I would've preferred to leave this as a comment, but I don't have enough reputation. By far the most succinct way to get AM or PM is using the ternary operator:

String am_pm = (hourOfDay < 12) ? "AM" : "PM";
衣神在巴黎 2024-12-13 01:23:22

我使用日历做到了这一点,这应该适用于所有版本:

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 的使用知道窍门。

I did it using Calendar, and that should work on all versions:

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 );
}

Notice the use of Calendar.HOUR_OF_DAY and Calendar.HOUR if you go to the documentation of both you'd know the trick.

淡莣 2024-12-13 01:23:22

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

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();
        }
    };

Here I'm taking the current date and time from the system and updating it with date and time picker dialog

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);

      } 

Now create the dialogs and update the values
//Creating dialogs

    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();
        }
    };
脱离于你 2024-12-13 01:23:22

不确定这是否有帮助,但我正在做同样的事情。在这里查看我的回复
TimePickerDialog 和 AM 或 PM

Not sure if it'll help but I am kind of working on the same thing. See my reply here
TimePickerDialog and AM or PM

未蓝澄海的烟 2024-12-13 01:23:22

我认为回调将以 24 格式的小时调用,因为。 Dialog 在内部使用 TimePicker。并且 TimePickergetCurrentHour 方法,返回 0-23 格式的小时。

I think callback will be called with hour in 24-format because. Dialog uses TimePicker internally. And TimePicker has getCurrentHour moethod, which returns hour in 0-23 format.

迷迭香的记忆 2024-12-13 01:23:22

例如:

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

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

e.g :

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

Integer.toString(((sellerTime.getCurrentHour().intValue()) > 12) ?  ((sellerTime.getCurrentHour().intValue()) - 12) : 
(sellerTime.getCurrentHour().intValue())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文