码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2016-01-20 11:23:24      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:

时间选择器(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)——时间选择器

标签:

原文地址:http://blog.csdn.net/sweetloverft/article/details/50546305

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!