android TimePicker

Android中關於日期時間的類有TimePickerDatePicker、TimePickerDialog、DatePickerDialog、Calendar。其中TimePickerDialog、DatePickerDialog是對話框形式。java

1、TimePicker

  查看一個在24小時或上午/下午模式下一天的時間。android

1.重要方法
setCurrentMinute(Integer currentMinute)設置當前時間的分鐘

  getCurrentMinute()獲取當前時間的分鐘

  setEnabled(boolean enabled)設置當前視圖是否能夠編輯。

  m_TimePicker.setIs24HourView(true);設置爲24小時制顯示

  setOnTimeChangedListener(TimePicker.OnTimeChangedListener onTimeChangedListener)當時間改變時調用app

2、DatePicker

  1.重要方法

  getDayOfMonth():獲取當前Day

  getMonth():獲取當前月

  getYear()獲取當前年ide

3、TimePickerDialog、DatePickerDialog佈局


  以對話框形式顯示日期時間視圖

4、Calendar

  日曆是設定年度日期對象和一個整數字段之間轉換的抽象基類,如,月,日,小時等。


  updateDate(int year, int monthOfYear, int dayOfMonth):更新日期ui

 

=========================================this

案例定義一個佈局文件timepicker.xmlspa

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
  <TextView    
      android:id="@+id/timeDisplay"      
      android:layout_width="wrap_content"     
      android:layout_height="wrap_content"     
      android:text=""    />
  <Button     
      android:id="@+id/pickTime"    
      android:layout_width="wrap_content"    
      android:layout_height="wrap_content"    
      android:text="Change the time"    />

</LinearLayout>

2.java文件:TimePickerDemo.java

package com.test;
import java.util.Calendar;

import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;

public class TimePickerDemo extends Activity {
	
	private  TextView  mtextView;
	
	private  Button  mbutton;
	private  int mHour,mMinute;
	static  final int TIME_DIALOG=0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.timepicker);
		
		final Calendar  calendar =Calendar.getInstance();
		
		this.mHour=calendar.get(Calendar.HOUR_OF_DAY);
		
		this.mMinute=calendar.get(Calendar.MINUTE);
		
		mtextView=(TextView)findViewById(R.id.timeDisplay);
	    mbutton  =(Button)findViewById(R.id.pickTime);
	    
	    mbutton.setOnClickListener(clickListener);
	    
	}

	private  OnClickListener   clickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			showDialog(TIME_DIALOG);
		}
	};
	
	private void updateDisplay() {
		// TODO Auto-generated method stub
		mtextView.setText(new StringBuilder()
		                  .append(pad(mHour)).append(":")
		                  .append(pad(mMinute)));
	}
	
	private String  pad(int c) 
	{
		// TODO Auto-generated method stub
		if(c>=10)
		{
			return String.valueOf(c);
		}else
		{
			return "0"+String.valueOf(c);
		}
	}
	@Override
	protected Dialog onCreateDialog(int id) {
 
		switch (id) {
		case TIME_DIALOG:
			
			return new TimePickerDialog(this,mTimeSetListener,mHour,mMinute, false);

		
		}
		return null;
	}
	
	private OnTimeSetListener  mTimeSetListener=new OnTimeSetListener() {
		
		@Override
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			// TODO Auto-generated method stub
			 mHour= hourOfDay;                    
			 mMinute=minute;                    
			 updateDisplay();
		}
	};
}

相關文章
相關標籤/搜索