本文介绍了如何在上午和下午显示时间选择器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

EditText time = (EditText) findViewById(R.id.time);

time.setOnLongClickListener(new OnLongClickListener(){
    @Override
    public boolean onLongClick(View v) {
	// TODO Auto-generated method stub
	Calendar mcurrentTime = Calendar.getInstance();
	int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
	int minute = mcurrentTime.get(Calendar.MINUTE);
	TimePickerDialog mTimePicker;

	mTimePicker = new TimePickerDialog(AddEventsActivity.this, new TimePickerDialog.OnTimeSetListener() {

	      @Override
	      public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
	         time.setText( selectedHour + ":" + selectedMinute);
	        }
	 }, hour, minute, true);//Yes 24 hour time

	 mTimePicker.setTitle("Select Time");
	 mTimePicker.show();

	 return false;
         }
});



上面的代码显示24小时制,同时长按编辑文本。但是,我想要12小时制(即上午和下午)。



我应该在这段代码中更改什么?还有其他方法吗?


The above code display 24 hour clock while long click on edit text. But, I want 12 hour clock (i.e. with am and pm).

What should I change in this code? Is there any other way?

推荐答案

mTimePicker = new TimePickerDialog(AddEventsActivity.this, new TimePickerDialog.OnTimeSetListener() {...}, hour, minute, false);



参考: []


<pre>
EditText time = (EditText) findViewById(R.id.time);

time.setOnLongClickListener(new OnLongClickListener(){
        @Override
	public boolean onLongClick(View v) {
	// TODO Auto-generated method stub
	Calendar mcurrentTime = Calendar.getInstance();
	int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
	int minute = mcurrentTime.get(Calendar.MINUTE);
	TimePickerDialog mTimePicker;

	mTimePicker = new TimePickerDialog(AddEventsActivity.this, new TimePickerDialog.OnTimeSetListener() {
	     @Override
	     public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {

	        String am_pm;
	        if (selectedHour > 12)
	          {
	             selectedHour = selectedHour - 12;
	             am_pm = "PM";
	             } else {
	             am_pm = "AM";
	          }

	      time.setText( selectedHour + ":" + selectedMinute + " " +am_pm);
	     }
	     }, hour, minute, false);
	     mTimePicker.setTitle("Select Time");
	     mTimePicker.show();
             return false;
            }
});


这篇关于如何在上午和下午显示时间选择器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 20:18