Android中TimePicker與DatePicker時(shí)間日期選擇組件的使用實(shí)例
效果和代碼都非常直觀:
實(shí)例1:TimePicker

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TimePicker
android:id="@+id/timePic1"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
<Button
android:id="@+id/buttone1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/timePic1"
android:text="獲取TimePick時(shí)間"/>
</RelativeLayout>
package com.android.xiong.times;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
public class MainActivity extends Activity {
private TimePicker timePick1;
private Button buttone1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timePick1=(TimePicker)findViewById(R.id.timePic1);
buttone1=(Button)findViewById(R.id.buttone1);
OnChangeListener buc=new OnChangeListener();
buttone1.setOnClickListener(buc);
//是否使用24小時(shí)制
timePick1.setIs24HourView(true);
TimeListener times=new TimeListener();
timePick1.setOnTimeChangedListener(times);
}
class OnChangeListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int h=timePick1.getCurrentHour();
int m=timePick1.getCurrentMinute();
System.out.println("h:"+h+" m:"+m);
}
}
class TimeListener implements OnTimeChangedListener{
/**
* view 當(dāng)前選中TimePicker控件
* hourOfDay 當(dāng)前控件選中TimePicker 的小時(shí)
* minute 當(dāng)前選中控件TimePicker 的分鐘
*/
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
System.out.println("h:"+ hourOfDay +" m:"+minute);
}
}
@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;
}
}
實(shí)例2:DatePicker

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<DatePicker
android:id="@+id/datePick1"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<Button
android:id="@+id/button1"
android:layout_below="@id/datePick1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="獲取DatePicker的值"/>
</RelativeLayout>
package com.android.xiong.datepicker;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
public class MainActivity extends Activity {
private DatePicker datePicker1;
private Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
datePicker1=(DatePicker)findViewById(R.id.datePick1);
//設(shè)置默認(rèn)的時(shí)間 比如2055年 9月9日
datePicker1.updateDate(2012, 8, 9);
button1=(Button)findViewById(R.id.button1);
OnClicLisers cl=new OnClicLisers();
button1.setOnClickListener(cl);
}
class OnClicLisers implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int y=datePicker1.getYear();
int m=datePicker1.getMonth()+1;
int d=datePicker1.getDayOfMonth();
System.out.println("y:"+y+" m:"+m+" d:"+d);
}
}
@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;
}
}
- Android實(shí)現(xiàn)日期時(shí)間選擇對(duì)話框
- Android 自定義日期段選擇控件功能(開始時(shí)間-結(jié)束時(shí)間)
- Android開發(fā)之DatePicker和TimePicker實(shí)現(xiàn)選擇日期時(shí)間功能示例
- Android仿iPhone日期時(shí)間選擇器詳解
- Android日期和時(shí)間選擇器實(shí)現(xiàn)代碼
- Android之日期時(shí)間選擇控件DatePicker和TimePicker實(shí)例
- Android開發(fā)中實(shí)現(xiàn)IOS風(fēng)格底部選擇器(支持時(shí)間 日期 自定義)
- Android時(shí)間選擇器、日期選擇器實(shí)現(xiàn)代碼
- Android之日期及時(shí)間選擇對(duì)話框用法實(shí)例分析
- Android開發(fā)實(shí)現(xiàn)日期時(shí)間控件選擇
相關(guān)文章
android實(shí)現(xiàn)人臉識(shí)別技術(shù)的示例代碼
本篇文章主要介紹了android人臉識(shí)別技術(shù)的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03
Android 布局控件之LinearLayout詳細(xì)介紹
Android 布局控件之LinearLayout詳細(xì)介紹,需要的朋友可以參考一下2013-05-05
Android三種實(shí)現(xiàn)定時(shí)器的方法
本文給大家分享了3種Android實(shí)現(xiàn)定時(shí)器的方法的示例,,需要的朋友可以參考下2015-02-02
Android ndk獲取手機(jī)內(nèi)部存儲(chǔ)卡的根目錄方法
今天小編就為大家分享一篇Android ndk獲取手機(jī)內(nèi)部存儲(chǔ)卡的根目錄方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08
詳解Android .9.png “點(diǎn)九”圖片的使用
這篇文章主要為大家詳細(xì)介紹了Android .9.png “點(diǎn)九”圖片的使用方法,感興趣的小伙伴們可以參考一下2016-09-09
Android 使用Vitamio打造自己的萬能播放器(9)—— 在線播放 (在線電視)
本文主要介紹Android 使用Vitamio開發(fā)播放器,實(shí)現(xiàn)在線電視播放,這里提供效果圖和實(shí)例代碼以便大家參考,2016-07-07
Android BadgeView紅點(diǎn)更新信息提示示例代碼
本篇文章主要介紹了Android BadgeView紅點(diǎn)更新信息提示示例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01
Android中實(shí)現(xiàn)Runnable接口簡單例子
這篇文章主要介紹了Android中實(shí)現(xiàn)Runnable接口簡單例子,著重點(diǎn)在如何實(shí)現(xiàn)run()方法,需要的朋友可以參考下2014-06-06
Android實(shí)現(xiàn)View滑動(dòng)的6種方式
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)View滑動(dòng)的6種方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05

