欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

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

时间:2021-10-09 08:51:12|栏目:Android代码|点击:

本文实例分析了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

运行效果:

如果:

return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute, false);

代码部分:

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程序设计有所帮助。

上一篇:浅谈谈Android 图片选择器

栏    目:Android代码

下一篇:Android 调用系统相机拍摄获取照片的两种方法实现实例

本文标题:Android开发之TimePicker控件用法实例详解

本文地址:http://www.codeinn.net/misctech/179263.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有