Android开发之TimePicker控件用法实例详解
作者:Hongten 时间:2022-02-10 10:18:50
本文实例分析了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,TimePicker
0
投稿
猜你喜欢
C#将Sql数据保存到Excel文件中的方法
2023-11-11 08:34:26
C#定义简单的反射工厂实例分析
2021-08-21 15:16:16
Unity Shader实现序列帧动画效果
2021-10-31 22:50:18
java返回json请求中文变成问号的问题及解决
2023-11-01 02:56:34
Spring Boot和Hazelcast使用详解
2021-06-08 11:28:02
详细解读C++编程中的匿名类类型和位域
2023-11-02 23:08:18
Java多态和实现接口的类的对象赋值给接口引用的方法(推荐)
2023-11-26 11:59:41
Android使用phonegap从相册里面获取照片(代码分享)
2023-07-24 18:53:03
JFinal实现伪静态的方法
2023-07-17 12:11:37
用intellij Idea加载eclipse的maven项目全流程(图文)
2021-09-12 06:11:16
Android RecyclerView的简单使用
2022-08-07 22:04:23
Android实现多线程下载图片的方法
2021-10-16 08:37:43
Android自定义控件实现时间轴
2021-07-12 04:13:08
WPF中使用CallerMemberName简化InotifyPropertyChanged的实现
2023-05-08 16:27:07
Java集合使用 Iterator 删除元素
2022-02-25 12:32:44
c#生成验证码程序
2023-01-08 18:38:59
c#二叉树存储介绍
2022-11-02 07:58:30
Java解析Excel内容的方法
2023-03-20 20:36:26
SpringBoot+EasyPoi实现excel导出功能
2022-05-08 06:47:35
Android Studio 3.0的下载安装教程
2021-08-10 04:00:45