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(安卓)程序运行之后,禁止休眠
  2. Android(安卓)Studio 项目运行错误,弹出“Detected ADB对话框”
  3. Android:开发环境搭建
  4. 【总结备用】Android(安卓)获取正在运行的任务和服务
  5. android 获取root修改系统时间
  6. android 如何判断程序是否在前台运行
  7. Android创建快捷方式(shortcut)
  8. android 下的webview 设置多点触控放大
  9. android启动:app_process实现恢复出厂设置可恢复的apk预置

随机推荐

  1. lambda表达式进行对象结合操作的实例详解
  2. 最新开源DBLayer的详细介绍
  3. IIS中出现了时间格式转换错误该如何解决?
  4. C#中匿名委托以及Lambda表达式的实例详解
  5. WPF实现简单的进度条怎么做?
  6. Oracle Clob字段过长保存出错改如何解决?
  7. C# 利用委托进行异步处理实例代码
  8. 带你了解CLR是如何创建运行时对象?
  9. 高性能缓存系统(Memcached)的实例介绍
  10. c#中Noto Sans字体支持韩文的实例教程