以hh:mm格式显示时间选择器 [英] Show the time picker in hh:mm format

查看:206
本文介绍了以hh:mm格式显示时间选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在单击编辑文本时以hh:mm格式显示时间选择器。这样我就使用了下面的代码。

I have to show the time picker in hh:mm format on Click the edit text.So that I had used the below code.

MainActivity。 java:

  e6.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                    // TODO Auto-generated method stub
                    SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
                    Calendar mcurrentTime = Calendar.getInstance();
                    int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
                    int minute = mcurrentTime.get(Calendar.MINUTE);
                    TimePickerDialog mTimePicker;
                    mTimePicker = new TimePickerDialog(AddFlight.this, new TimePickerDialog.OnTimeSetListener() {
                        @Override
                        public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
                            e6.setText(selectedHour + ":" + selectedMinute);

                        }
                    }, hour, minute, true);//Yes 24 hour time
                    mTimePicker.setTitle("Select Time");
                    mTimePicker.show();

            }
        });

它运行良好并显示时间选择器。但是我的 issue 是,当我在时间选择器中选择04:15时,它将显示4:15。在4之前不包含0,我需要以hh:mm格式显示时间选择器。

It is working well and shows the time picker.But my issue is,when I select the 04:15 in timepicker, it display 4:15.It doesn't include 0 before 4.I need to show the timepicker in hh:mm format.

任何人都可以帮助我。谢谢。

Anyone can help me with this.Thank you.

推荐答案

刚才我已经提到了这个发布并更改单行:

Just now I had referred this post and change the single line:

此行


e6.setText(selectedHour +: + selectedMinute);

e6.setText(selectedHour + ":" + selectedMinute);


e6.setText(String.format(%02d :%02d,selectedHour,
selectedMinute));

e6.setText(String.format("%02d:%02d", selectedHour, selectedMinute));

这篇关于以hh:mm格式显示时间选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆