Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 閱讀《Android 從入門到精通》(14)——時間選擇器

閱讀《Android 從入門到精通》(14)——時間選擇器

編輯:關於Android編程

時間選擇器(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

<!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-8%22%3F%2D%2D%3E-->
<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">

</textview></timepicker></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">
            </category></action></intent-filter>
        </activity>
    </application>

</uses-sdk></manifest>
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved