本文主要是介绍Android控件开发之TimePicker,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
TimePicker介绍
TimePicker也继承自FrameLayout类. 时间选择控件向用户显示一天中的时间
(可以为24小时,也可以为AM/PM制),并允许用户进行选择.如果要捕获用户修改的时间数据事件,
便需要为TimePicker添加OnTimeChangedListener监听器
- setCurrentMinute(Integer currentMinute) 设置当前时间的分钟
- getCurrentMinute() 获取当前时间的分钟
- setEnabled(boolean enabled) 设置当前视图是否可以编辑。
- m_TimePicker.setIs24HourView(true); 设置为24小时制显示
- setOnTimeChangedListener(TimePicker.OnTimeChangedListener onTimeChangedListener)当时间改变时调用
2.实例:
- timePicker = (TimePicker)findViewById(R.id.timePicker);
- timePicker.setCurrentHour(16);
- timePicker.setCurrentMinute(10);
- updateDisplay(16,10);
- timePicker.setOnTimeChangedListener(this);
效果图
本程序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:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="日期选择控件"/>
- <TimePicker
- android:id="@+id/timePicker"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"/>
- <TextView
- android:id="@+id/textView"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical"/>
- </LinearLayout>
Java源码
- package com.sx.TimePicker;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.TextView;
- import android.widget.TimePicker;
- import android.widget.TimePicker.OnTimeChangedListener;
- public class TimePickerActivity extends Activity
- {
- private TimePicker timePicker = null;
- private TextView textView = null;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- timePicker = (TimePicker)findViewById(R.id.timePicker);
- textView = (TextView) findViewById(R.id.textView);
- timePicker.setOnTimeChangedListener(new OnTimeChangedListener()
- {
- @Override
- public void onTimeChanged(TimePicker view, int hourOfDay, int minute)
- {
- textView.setText("你选择的时间是: " + hourOfDay + "时" + minute + "分");
- }
- });
- }
- }
TimePickerDialog
以对话框形式显示时间视图
效果图
Java源码
- package com.sx.TimePickerDialog;
- import java.util.Calendar;
- import android.app.Activity;
- import android.app.DatePickerDialog;
- import android.app.TimePickerDialog;
- import android.os.Bundle;
- import android.widget.DatePicker;
- import android.widget.TimePicker;
- public class TimePickerDialogActivity extends Activity
- {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- final Calendar calendar = Calendar.getInstance();
- final int hour = calendar.get(Calendar.HOUR_OF_DAY);
- final int minute = calendar.get(Calendar.MINUTE);
- final TimePickerDialog timePickerDialog = new TimePickerDialog(this, setting, hour, minute, true);
- timePickerDialog.show();
- }
- //当点击TimePickerDialog控件的设置按钮时,调用该方法
- TimePickerDialog.OnTimeSetListener setting = new TimePickerDialog.OnTimeSetListener()
- {
- @Override
- public void onTimeSet(TimePicker view, int hour, int minute)
- {
- }
- };
- }
这篇关于Android控件开发之TimePicker的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!