android时间选择器使用方法,android 时间选择器(TimePicker )学习与应用

廖鸿达
2023-12-01

1、新建工程

2、修改修改已经生成的res/layout/main.xml,整体替换为:

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

android:textSize="20sp" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="设置日期"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:gravity="right"

android:text="设置时间"/>

3、添加关键代码

import java.util.Calendar;

import android.app.Activity;

import android.app.DatePickerDialog;

import android.app.Dialog;

import android.app.TimePickerDialog;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.DatePicker;

import android.widget.TextView;

import android.widget.TimePicker;

public class Timepicker extends Activity {

private TextView mDateDisplay;

private Button button,button1;

private int mYear;

private int mMonth;

private int mDay;

private int h;

private int m;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

mDateDisplay = (TextView) findViewById(R.id.dateDisplay);

button = (Button)findViewById(R.id.button);

button1 = (Button)findViewById(R.id.button1);

//设置日期选择监听

button.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

showDialog(0);

}

});

//设置时间选择监听

button1.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

showDialog(1);

}

});

final Calendar c = Calendar.getInstance();

mYear = c.get(Calendar.YEAR);

mMonth = c.get(Calendar.MONTH);

mDay = c.get(Calendar.DAY_OF_MONTH);

h = c.get(Calendar.HOUR_OF_DAY);

m = c.get(Calendar.MINUTE);

//显示当前时间

updateDisplay();

}

@Override

protected Dialog onCreateDialog(int id) {

switch (id) {

case 0:

return new DatePickerDialog(this,

mDateSetListener,

mYear, mMonth, mDay);

case 1:

return new TimePickerDialog(this, mtimesetListener, h, m, true);

}

return null;

}

// 将时间显示到文本框中

private void updateDisplay() {

mDateDisplay.setText(

new StringBuilder().append(mYear).append("-")

.append(formateTime(mMonth+1)).append("-")

.append(formateTime(mDay)).append(" ")

.append(formateTime(h)).append(":")

.append(formateTime(m)));

}

//获得当前日期

private DatePickerDialog.OnDateSetListener mDateSetListener =

new DatePickerDialog.OnDateSetListener() {

public void onDateSet(DatePicker view, int year,

int monthOfYear, int dayOfMonth) {

mYear = year;

mMonth = monthOfYear;

mDay = dayOfMonth;

updateDisplay();

}

};

//获得当前时间

private TimePickerDialog.OnTimeSetListener mtimesetListener =

new TimePickerDialog.OnTimeSetListener(){

@Override

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

// TODO Auto-generated method stub

h = hourOfDay;

m = minute;

updateDisplay();

}

};

private String formateTime(int time) {

String timeStr = "";

if (time < 10){

timeStr = "0" + String.valueOf(time);

}else {

timeStr = String.valueOf(time);

}

return timeStr;

}

}

小结:

配置文件里没有直接使用Datepicker、TimePicker等标签。而是用按钮控制Dialog来显示日期、时间。这两种类型的对象最大的差别在于DatePicker与TimePicker是直接显示在屏幕画面上,而DatePickerDialog与TimePickerDialog对象则是以跳出Dialog的方式来显示。

 类似资料: