欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android开发之TimePicker控件用法实例详解

程序员文章站 2024-02-25 10:28:10
本文实例分析了android开发之timepicker控件用法。分享给大家供大家参考,具体如下: 新建项目: new android project-> pro...

本文实例分析了android开发之timepicker控件用法。分享给大家供大家参考,具体如下:

新建项目:

new android project->
project name:hellospinner
build target:android 2.2
application name:hellospinner
package name:com.b510
create activity:mainactivity
min sdk version:9
finish

运行效果:

Android开发之TimePicker控件用法实例详解

如果:

return new timepickerdialog(this, mtimesetlistener, mhour, mminute, false);

Android开发之TimePicker控件用法实例详解

Android开发之TimePicker控件用法实例详解

代码部分:

mainactivity.java:

package com.b510;
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.view.view.onclicklistener;
import android.widget.button;
import android.widget.textview;
import android.widget.timepicker;
public class hellotimepicker extends activity {
  /* 显示时间信息 */
  private textview tvtimepickerdisplay;
  /* 设置时间按钮 */
  private button btntimepicker;
  /* 小时 */
  private int mhour;
  /* 分钟 */
  private int mminute;
  /* 标识 dialog的id */
  static final int time_dialog_id = 0;
  /** called when the activity is first created. */
  @override
  public void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.main);
    this.tvtimepickerdisplay = (textview) this
        .findviewbyid(r.id.tv_timepicker_display);
    this.btntimepicker = (button) findviewbyid(r.id.btn_timepicker);
    btntimepicker.setonclicklistener(listener);
    // 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();
  }
  private onclicklistener listener = new onclicklistener() {
    @override
    public void onclick(view v) {
      showdialog(time_dialog_id);
    }
  };
  // updates the time we display in the textview
  private void updatedisplay() {
    tvtimepickerdisplay.settext(new stringbuilder().append(pad(mhour)).append(":")
        .append(pad(mminute)));
  }
  private static string pad(int c) {
    if (c >= 10)
      return string.valueof(c);
    else
      return "0" + string.valueof(c);
  }
  // the callback received when the user "sets" the time in the dialog
  private timepickerdialog.ontimesetlistener mtimesetlistener = new timepickerdialog.ontimesetlistener() {
    public void ontimeset(timepicker view, int hourofday, int minute) {
      mhour = hourofday;
      mminute = minute;
      updatedisplay();
    }
  };
  @override
  protected dialog oncreatedialog(int id) {
    switch (id) {
    case time_dialog_id:
      return new timepickerdialog(this, mtimesetlistener, mhour, mminute,
          true);
    }
    return null;
  }
}

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
<textview 
  android:id="@+id/tv_timepicker_display"
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
/>
<button
  android:id="@+id/btn_timepicker"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="change the time"
/>
</linearlayout>

更多关于android相关内容感兴趣的读者可查看本站专题:《android开发入门与进阶教程》、《android多媒体操作技巧汇总(音频,视频,录音等)》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。