Android開發(fā)之時(shí)間日期組件用法實(shí)例
繼上一篇時(shí)間和日期設(shè)置的示例之后,今天來介紹Android的布局組件中有關(guān)于時(shí)間和日期的設(shè)置的組件,希望對(duì)大家有所幫助。具體如下:
時(shí)間日期設(shè)置組件:TimePicker、DatePicker
在布局文件中直接可以添加到我們的布局樣式中,具體代碼如下:
<LinearLayout
android:id="@+id/linear1"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="@+id/editview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/linear2"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
><br> //時(shí)間設(shè)置組件
<TimePicker
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/> <br> //日期設(shè)置組件
<DatePicker
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
.java文件代碼如下:
public class MainActivity extends Activity {
private EditText myedit = null;<br> //對(duì)兩個(gè)組件進(jìn)行聲明
private TimePicker time = null;
private DatePicker date = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);<br><br> //設(shè)置Activity的顯示方向?yàn)闄M屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
myedit = (EditText)findViewById(R.id.editview);<br> //調(diào)用布局文件中的兩個(gè)組件
time = (TimePicker)findViewById(R.id.time);
date = (DatePicker)findViewById(R.id.date);
//time.setIs24HourView(true);//設(shè)置采用24小時(shí)制的時(shí)間顯示,系統(tǒng)默認(rèn)12小時(shí)制
time.setOnTimeChangedListener(new mytime());<br> //在這里需要注意的是日期沒有ondatechangedlistener()方法,我們這里通過調(diào)用日期的init()方法設(shè)置。
date.init(this.date.getYear(), this.date.getMonth(), this.date.getDayOfMonth(), new myDate());
this.setDateTime();
}
//時(shí)間改變事件監(jiān)聽: <br><br> class mytime implements OnTimeChangedListener{
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
MainActivity.this.setDateTime();
}
}
<br> //日期改變事件監(jiān)聽
@SuppressLint("NewApi")
class myDate implements OnDateChangedListener{
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
MainActivity.this.setDateTime();
}
}
public void setDateTime() { // 由于日期時(shí)間更改之后文本輸入組件的內(nèi)容也要修改
this.myedit.setText(this.date.getYear() + "-"
+ (this.date.getMonth() + 1) + "-" + this.date.getDayOfMonth()
+ " " + this.time.getCurrentHour() + ":"
+ this.time.getCurrentMinute()); // 修改文本的內(nèi)容
}
}
效果圖如下:
這個(gè)例子中比較難的點(diǎn)在于,日期事件沒有OnChangedListener()方法,在這里我們使用了日期的init()方法來代替。其次就是如何控制Activity的顯示方向,Activity的顯示方向默認(rèn)是豎屏顯示,我們可以通過java文件從新設(shè)置,也可以在配置文件中進(jìn)行設(shè)置,這也是java文件中設(shè)置比較靈活之處。
- 解析android中系統(tǒng)日期時(shí)間的獲取
- Android時(shí)間選擇器、日期選擇器實(shí)現(xiàn)代碼
- Android中日期與時(shí)間設(shè)置控件用法實(shí)例
- Android日期時(shí)間格式國際化的實(shí)現(xiàn)代碼
- Android開發(fā)之時(shí)間日期操作實(shí)例
- Android之日期及時(shí)間選擇對(duì)話框用法實(shí)例分析
- Android日期和時(shí)間選擇器實(shí)現(xiàn)代碼
- Android仿iPhone日期時(shí)間選擇器詳解
- Android開發(fā)中DatePicker日期與時(shí)間控件實(shí)例代碼
- Android 日期和時(shí)間的使用實(shí)例詳解
- Android開發(fā)獲取當(dāng)前系統(tǒng)日期和時(shí)間功能示例
相關(guān)文章
Android實(shí)現(xiàn)簡(jiǎn)易記事本
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)易記事本,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07
Android開發(fā)在RecyclerView上面實(shí)現(xiàn)"拖放"和"滑動(dòng)刪除"-2
這篇文章主要介紹了Android開發(fā)在RecyclerView上面實(shí)現(xiàn)"拖放"和"滑動(dòng)刪除"(二)功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03
Android實(shí)現(xiàn)流光和光影移動(dòng)效果代碼
大家好,本篇文章主要講的是Android實(shí)現(xiàn)流光和光影移動(dòng)效果代碼,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12
Android中findViewById獲取控件返回為空問題怎么解決
這篇文章主要介紹了Android中findViewById獲取控件返回為空問題怎么解決的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06
Android自定義實(shí)現(xiàn)BaseAdapter的普通實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Android自定義實(shí)現(xiàn)BaseAdapter的普通實(shí)現(xiàn),感興趣的小伙伴們可以參考一下2016-08-08
Android動(dòng)畫之3D翻轉(zhuǎn)效果實(shí)現(xiàn)函數(shù)分析
Android中的翻轉(zhuǎn)動(dòng)畫效果的實(shí)現(xiàn),Android中并沒有提供直接做3D翻轉(zhuǎn)的動(dòng)畫,所以關(guān)于3D翻轉(zhuǎn)的動(dòng)畫效果需要我們自己實(shí)現(xiàn),那么我們首先來分析一下Animation 和 Transformation,感興趣的朋友可以了解下啊2013-01-01
詳談自定義View之GridView單選 金額選擇Layout-ChooseMoneyLayout
下面小編就為大家?guī)硪黄斦勛远xView之GridView單選 金額選擇Layout-ChooseMoneyLayout。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05

