我是靠谱客的博主 自然小鸭子,最近开发中收集的这篇文章主要介绍android内置picker控件,Android开发之TimePicker控件用法实例详解,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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

运行效果:

b7ebcc6c9e520394b959a48caff2c890.png

如果:

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

95e2e0435f237cd1291a7eed01583211.png

d46d4e3eb7570079f257579ded401ccd.png

代码部分:

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:

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

android:id="@+id/tv_timepicker_display"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

/>

android:id="@+id/btn_timepicker"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="Change the time"

/>

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

最后

以上就是自然小鸭子为你收集整理的android内置picker控件,Android开发之TimePicker控件用法实例详解的全部内容,希望文章能够帮你解决android内置picker控件,Android开发之TimePicker控件用法实例详解所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(84)

评论列表共有 0 条评论

立即
投稿
返回
顶部