Android時(shí)間選擇器、日期選擇器實(shí)現(xiàn)代碼
本文為大家分享了兩款選擇器,一款可以針對(duì)時(shí)間進(jìn)行選擇、一款可以針對(duì)日期進(jìn)行選擇,供大家參考,具體內(nèi)容如下
一、時(shí)間選擇器
1.1.布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" tools:context="com.rj141.sb.kongjian.DateActivity"> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:text="幾點(diǎn)吃飯:" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:id="@+id/tv" /> </LinearLayout> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="時(shí)間" android:id="@+id/btndate" /> </LinearLayout>
1.2.Java文件
public class DateActivity extends ActionBarActivity {
private Button btn;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_date);
btn=(Button)this.findViewById(R.id.btndate);
tv= (TextView) this.findViewById(R.id.tv);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new TimePickerDialog(DateActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
tv.setText(String.format("%d:%d",hourOfDay,minute));
}
//0,0指的是時(shí)間,true表示是否為24小時(shí),true為24小時(shí)制
},0,0,true).show();
}
});
}
} 效果圖:

二、日期選擇器
2.1.activity.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" tools:context="com.rj141.sb.kongjian.DateActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20dp" android:id="@+id/tv" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="日歷" android:id="@+id/btndate" /> </LinearLayout>
2.2.DateActivity.class
public class DateActivity extends ActionBarActivity {
private Button btn;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_date);
btn=(Button)this.findViewById(R.id.btndate);
tv= (TextView) this.findViewById(R.id.tv);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DatePickerDialog(DateActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
tv.setText("您的出生日期是:"+String.format("%d-%d-%d",year,monthOfYear+1,dayOfMonth));
}
},2000,1,2).show();
}
});
}
}
DatePickerDialog日歷選擇器的對(duì)話框,監(jiān)聽(tīng)為OnDateSetListener(){..}
效果圖:

以上就是兩款A(yù)ndroid時(shí)間選擇器、Android日期選擇器的實(shí)現(xiàn)代碼,希望對(duì)大家學(xué)習(xí)Android軟件編程有所幫助。
- Android自定義Material進(jìn)度條效果
- Material Design系列之Behavior實(shí)現(xiàn)Android知乎首頁(yè)
- Android App仿QQ制作Material Design風(fēng)格沉浸式狀態(tài)欄
- Android Material設(shè)計(jì)中列表和卡片的創(chuàng)建方法解析
- 詳解Android Material設(shè)計(jì)中陰影效果的實(shí)現(xiàn)方法
- 詳解Android Material Design自定義動(dòng)畫的編寫
- Android?Material組件庫(kù)日期選擇和時(shí)間選擇器的使用方法
相關(guān)文章
Android開(kāi)發(fā)教程之Fragment定義、創(chuàng)建與使用方法詳解【包含Activity通訊,事務(wù)執(zhí)行等】
這篇文章主要介紹了Android開(kāi)發(fā)教程之Fragment定義、創(chuàng)建與使用方法,詳細(xì)介紹了Fragment的概念、功能、定義、創(chuàng)建及使用方法,包括Fragment與Activity通訊,Fragment事務(wù)執(zhí)行及Fragment應(yīng)用示例等,需要的朋友可以參考下2017-11-11
Android共享元素動(dòng)畫效果顯示問(wèn)題解決
什么是共享元素呢?可以理解為當(dāng)頁(yè)面跳轉(zhuǎn)是,看起來(lái)一個(gè)View屬于界面A又屬于界面B,下面這篇文章主要給大家介紹了關(guān)于Android共享元素動(dòng)畫效果顯示問(wèn)題的相關(guān)資料,需要的朋友可以參考下2022-02-02
Android編程中selector背景選擇器用法實(shí)例分析
這篇文章主要介紹了Android編程中selector背景選擇器用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Selector的結(jié)構(gòu)描述與使用技巧,需要的朋友可以參考下2016-01-01
Android Studio 恢復(fù)小窗口??磕J?Docked Mode)
這篇文章主要介紹了Android Studio 恢復(fù)小窗口停靠模式(Docked Mode),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04
Android實(shí)現(xiàn)雷達(dá)View效果的示例代碼
這篇文章主要介紹了Android實(shí)現(xiàn)雷達(dá)View效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
Android學(xué)習(xí)小結(jié)之Activity保存和恢復(fù)狀態(tài)
這篇文章主要介紹了Activity狀態(tài)保存和恢復(fù)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08
TabLayout+ViewPager2的簡(jiǎn)單使用詳解
這篇文章主要為大家詳細(xì)介紹了TabLayout+ViewPager2的簡(jiǎn)單使用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09

