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 自定義日期段選擇控件功能(開(kāi)始時(shí)間-結(jié)束時(shí)間)
- Android開(kāi)發(fā)之DatePicker和TimePicker實(shí)現(xiàn)選擇日期時(shí)間功能示例
- Android仿iPhone日期時(shí)間選擇器詳解
- Android日期和時(shí)間選擇器實(shí)現(xiàn)代碼
- Android之日期時(shí)間選擇控件DatePicker和TimePicker實(shí)例
- Android開(kāi)發(fā)中實(shí)現(xiàn)IOS風(fēng)格底部選擇器(支持時(shí)間 日期 自定義)
- Android時(shí)間選擇器、日期選擇器實(shí)現(xiàn)代碼
- Android之日期及時(shí)間選擇對(duì)話框用法實(shí)例分析
- Android開(kāi)發(fā)實(shí)現(xiàn)日期時(shí)間控件選擇
相關(guān)文章
Android ListView與getView調(diào)用卡頓問(wèn)題解決辦法
這篇文章主要介紹了Android ListView與getView調(diào)用卡頓問(wèn)題解決辦法的相關(guān)資料,這里提供實(shí)例及解決辦法幫助大家解決這種問(wèn)題,需要的朋友可以參考下2017-08-08解析Java的迭代器中的fast-fail錯(cuò)誤檢測(cè)機(jī)制
這篇文章主要介紹了Java的迭代器中的fast-fail錯(cuò)誤檢測(cè)機(jī)制,需要的朋友可以參考下2016-02-02Android studio4.1更新后出現(xiàn)的問(wèn)題詳解
這篇文章主要介紹了Android studio4.1更新后出現(xiàn)的問(wèn)題詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10Android 播放視頻常見(jiàn)問(wèn)題小結(jié)
這篇文章主要介紹了Android 播放視頻常見(jiàn)問(wèn)題小結(jié),需要的朋友可以參考下2017-04-04android接收到藍(lán)牙配對(duì)請(qǐng)求時(shí)如何點(diǎn)亮屏幕具體實(shí)現(xiàn)
android 在接收到藍(lán)牙配對(duì)請(qǐng)求時(shí)如何自動(dòng)點(diǎn)亮屏幕配對(duì)過(guò)程中很實(shí)用,具體的實(shí)現(xiàn)思路及代碼如下,感興趣的朋友可以參考下哈2013-06-06Android短信驗(yàn)證碼倒計(jì)時(shí)驗(yàn)證的2種常用方式
各位開(kāi)發(fā)者們?cè)陂_(kāi)發(fā)中經(jīng)常會(huì)遇到獲取短信驗(yàn)證碼,獲取驗(yàn)證碼后需要等待1分鐘倒計(jì)時(shí),這段時(shí)間是不能再次發(fā)送短信請(qǐng)求的。這篇文章總結(jié)了兩種常用的Android​短信驗(yàn)證碼倒計(jì)時(shí)驗(yàn)證方式,有需要的朋友們可以參考借鑒,下面來(lái)一起看看吧。2016-12-12Android實(shí)現(xiàn)左側(cè)滑動(dòng)菜單
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)左側(cè)滑動(dòng)菜單,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02