Hello, TimePicker

A TimePicker is a widget that allows the user to select the time by hour, minute and AM or PM.

Start a new project/Activity called HelloTimePicker.

修改 HelloTimePicker.java 代码如下:

package com.example.test;import java.util.Calendar;import android.app.Activity;import android.app.Dialog;import android.app.TimePickerDialog;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;import android.widget.TimePicker;public class HelloTimePicker extends Activity {private TextView mTimeDisplay;private Button mPickTime;private int mHour;private int mMinute;static final int TIME_DIALOG_ID = 0;@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);        // capture our View elements    mTimeDisplay = (TextView) findViewById(R.id.timeDisplay);    mPickTime = (Button) findViewById(R.id.pickTime);    // add a click listener to the button    mPickTime.setOnClickListener(new View.OnClickListener() {        public void onClick(View v) {            showDialog(TIME_DIALOG_ID);        }    });    // get the current time    final Calendar c = Calendar.getInstance();    mHour = c.get(Calendar.HOUR_OF_DAY);    mMinute = c.get(Calendar.MINUTE);    // display the current date    updateDisplay();}@Overrideprotected Dialog onCreateDialog(int id) {    switch (id) {    case TIME_DIALOG_ID:        return new TimePickerDialog(this,                mTimeSetListener, mHour, mMinute, false);    }    return null;}// updates the time we display in the TextViewprivate void updateDisplay() {    mTimeDisplay.setText(        new StringBuilder()                .append(pad(mHour)).append(":")                .append(pad(mMinute)));}private TimePickerDialog.OnTimeSetListener mTimeSetListener =    new TimePickerDialog.OnTimeSetListener() {        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {            mHour = hourOfDay;            mMinute = minute;            updateDisplay();        }};    private static String pad(int c) {        if (c >= 10)            return String.valueOf(c);        else            return "0" + String.valueOf(c);    }}

Layout->main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    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>

运行 Now run it. 运行结果如下:

源码下载:HelloTimePicker.zip

更多相关文章

  1. Android(安卓)修改App中默认TextView的字体和颜色
  2. android 调用相册里的图片并返回
  3. Android全屏设置
  4. Android(安卓)计时器的实现
  5. android p cts camera测试 android.hardware.camera2.cts.Captur
  6. Android(安卓)设置字体大小和显示大小后界面混乱
  7. 【Android代码片段之三】TabActivity实现多页显示效果
  8. Android(安卓)扫描音乐文件、两种方式获取文件最新修改时间
  9. Android修改自定义Dialog的大小

随机推荐

  1. Android7.0启动SystemServer进程
  2. 在Android中使用logback-android日志框架
  3. Android布局管理器-使用TableLayout表格
  4. 在Android中afinal框架下实现sqlite数据
  5. Android中获取资源的id和url方法总结
  6. ubuntu 下基于源码的android平台搭建与内
  7. View事件体系(Android开发艺术探索读书笔
  8. android 简单的相册查看器
  9. Activity面试详解
  10. Android调试之LOG和LOGCAT详解