基本视图——TimePicker

作者:追风剑情 发布于:2015-8-31 21:46 分类:Android

示例一: 在活动中显示TimePicker

视图


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context="${relativePackage}.${activityClass}" >

    <TimePicker android:id="@+id/timePicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    
    <Button android:id="@+id/btnSet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="I am all set!"
        android:onClick="onClick" />

</LinearLayout>


代码


package com.example.androidtest;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TimePicker;
import android.widget.Toast;

public class BasicViewsActivity extends Activity {

	TimePicker timePicker;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_basic_views);
		
		timePicker = (TimePicker) findViewById(R.id.timePicker);
		timePicker.setIs24HourView(true);
	}
	
	public void onClick(View view) {
		Toast.makeText(getBaseContext(), "Time selected:" +
					timePicker.getCurrentHour() +//getCurrentHour()总是返回24小时格式
					":"+timePicker.getCurrentMinute(),
					Toast.LENGTH_SHORT).show();
	}
}


运行效果

ttttttttttttttttttt.png

示例二: 在一个对话框窗口中显示TimePicker

代码

package com.example.androidtest;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TimePicker;
import android.widget.Toast;
import android.app.Dialog;
import android.app.TimePickerDialog;
import java.text.SimpleDateFormat;
import java.util.Date;

public class BasicViewsActivity extends Activity {

	TimePicker timePicker;
	
	int hour, minute;
	static final int TIME_DIALOG_ID = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_basic_views);
		
		timePicker = (TimePicker) findViewById(R.id.timePicker);
		timePicker.setIs24HourView(true);
		
		showDialog(TIME_DIALOG_ID);
	}
	
	@Override
	protected Dialog onCreateDialog(int id)
	{
		switch(id){
		case TIME_DIALOG_ID:
			return new TimePickerDialog(this, mTimeSetListener, hour, minute, false);
		}
		return null;
	}
	
	private TimePickerDialog.OnTimeSetListener mTimeSetListener =
	new TimePickerDialog.OnTimeSetListener() {
		public void onTimeSet(TimePicker view, int hourOfDay, int minuteOfHour)
		{
			hour = hourOfDay;
			minute = minuteOfHour;
			
			SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm aa");
			Date date = new Date(0,0,0, hour, minute);
			String strDate = timeFormat.format(date);
			
			Toast.makeText(getBaseContext(), "You have selected "+strDate, 
					Toast.LENGTH_SHORT).show();
		}
	};
}

运行效果

t1.png

标签: Android

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号