阅读《Android 从入门到精通》(14)——时间选择器

Posted SweetLoverFT

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了阅读《Android 从入门到精通》(14)——时间选择器相关的知识,希望对你有一定的参考价值。

时间选择器(TimePicker)

java.lang.Object;
android.view.View;
android.view.ViewGroup;
android.widget.FrameLayout;
android.widget.TimePicker;

TimePicker 类方法


TimePick 示例

完整工程:http://download.csdn.net/detail/sweetloveft/9410965

下述程序主要学习 TimePicker 的用法,不使用 Calendar 的做法同上一篇相同,需注意 24 小时时间置的问题。

1.MainActivity.java

package com.sweetlover.activity;

import com.sweetlover.timepickerdemo.R;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;

public class MainActivity extends Activity 

	private TextView textView = null;
	private TimePicker timePicker = null;
	private int curHour = 8, curMinute = 30;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		textView = (TextView)findViewById(R.id.textView1);
		timePicker = (TimePicker)findViewById(R.id.timePicker1);
		
		timePicker.setIs24HourView(false);
		timePicker.setOnTimeChangedListener(new TimeChangedListener());		
	

	private class TimeChangedListener implements OnTimeChangedListener 

		@Override
		public void onTimeChanged(TimePicker view, int hourOfDay, int minute) 
			// TODO Auto-generated method stub
			curHour = hourOfDay;
			curMinute = minute;
			textView.setText("当前时间:" + curHour + " 时 " + curMinute + " 分");
		
	

2.activity_main.xml

<?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:padding="30dp"
    android:orientation="vertical" >

    <TimePicker
        android:id="@+id/timePicker1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

3.AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sweetlover.timepickerdemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity android:name="com.sweetlover.activity.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

以上是关于阅读《Android 从入门到精通》(14)——时间选择器的主要内容,如果未能解决你的问题,请参考以下文章

阅读《Android 从入门到精通》(31)——Intent

阅读《Android 从入门到精通》(20)——图片视图

阅读《Android 从入门到精通》(30)——字体

阅读《Android 从入门到精通》(16)——表状时钟

阅读《Android 从入门到精通》(34)——Intent Filter

阅读《Android 从入门到精通》(34)——Intent Filter