在Android中使用TimePicker(时间选择器),可以进行时间的快速调整,此类定义如下:
java.lang.Object
↳android.view.View
↳android.view.ViewGroup
↳android.widget.FrameLayout
↳android.widget.TimePicker.
常用方法
No.
|
方法
|
类型
|
描述
|
1
|
public Integer getCurrentHour()
|
普通
|
返回当前设置的小时
|
2
|
public Integer getCurrentMinute()
|
普通
|
返回当前设置的分钟
|
3
|
public boolean is24HourView()
|
普通
|
判断是否是24小时制
|
4
|
public void setCurrentHour(Integer currentHour)
|
普通
|
设置当前的小时数
|
5
|
public void setCurrentMinute(Integer currentMinute)
|
普通
|
设置当前的分钟
|
6
|
public void setEnabled(boolean enabled)
|
普通
|
设置是否可用
|
7
|
public void setIs24HourView(Boolean is24HourView)
|
普通
|
设置时间为24小时制
|
…………………………………………………………毫无美感的分割线…………………………………………………………
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TimePicker
android:id="@+id/time"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TimePicker
android:id="@+id/time2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
JAVA文件
package com.example.timepacker;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
import android.widget.TimePicker;
public class MainActivity extends Activity {
private TimePicker Mytime=null;
private TextView mytext=null;
int num=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.linearlayout);
this.Mytime=(TimePicker)super.findViewById(R.id.time);
Mytime.setIs24HourView(true);//设置24小时制
Mytime.setCurrentHour(22);//设置小时为22
Mytime.setCurrentMinute(56);//56分
num=this.Mytime.getCurrentHour();
this.mytext=(TextView)super.findViewById(R.id.text);
this.mytext.setText(Integer.toString(num));//TextView为当前时间
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
效果如下图
下节预报:
日期选择器:DatePicker的使用