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中使用CircleImageView和Cardview制作圓形頭像的方法
這篇文章主要介紹了Android中使用CircleImageView和Cardview制作圓形頭像的方法,簡(jiǎn)單介紹了CircleImageView和Cardview的使用,需要的朋友可以參考下2016-09-09Android開(kāi)發(fā)筆記之:Dialog的使用詳解
本篇文章是對(duì)Android中Dialog的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05Android studio配置國(guó)內(nèi)鏡像源的實(shí)現(xiàn)
這篇文章主要介紹了Android studio配置國(guó)內(nèi)鏡像源的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11Android實(shí)現(xiàn)在屏幕上移動(dòng)圖片的方法
這篇文章主要介紹了Android實(shí)現(xiàn)在屏幕上移動(dòng)圖片的方法,實(shí)例分析了Android操作圖片的相關(guān)技巧,需要的朋友可以參考下2015-06-06Android實(shí)現(xiàn)向Launcher添加快捷方式的方法
這篇文章主要介紹了Android實(shí)現(xiàn)向Launcher添加快捷方式的方法,涉及Android添加快捷方式的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09Android RecyclerView實(shí)現(xiàn)吸頂動(dòng)態(tài)效果流程分析
RecyclerView是Android一個(gè)更強(qiáng)大的控件,其不僅可以實(shí)現(xiàn)和ListView同樣的效果,還有優(yōu)化了ListView中的各種不足。其可以實(shí)現(xiàn)數(shù)據(jù)縱向滾動(dòng),也可以實(shí)現(xiàn)橫向滾動(dòng)(ListView做不到橫向滾動(dòng))。接下來(lái)講解RecyclerView的用法2022-12-12Android啟動(dòng)頁(yè)面定時(shí)跳轉(zhuǎn)的三種方法
這篇文章主要介紹了Android啟動(dòng)頁(yè)面定時(shí)跳轉(zhuǎn)的三種方法,實(shí)現(xiàn)打開(kāi)一個(gè)Android手機(jī)APP的歡迎界面后跳轉(zhuǎn)到指定界面的效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Pagerslidingtabstrip菜單標(biāo)題欄制作方法
這篇文章主要為大家詳細(xì)介紹了Pagerslidingtabstrip菜單標(biāo)題欄的制作方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10Android基于OpenCV實(shí)現(xiàn)圖像修復(fù)
實(shí)際應(yīng)用中,圖像常常容易受損,如存在污漬的鏡頭、舊照片的劃痕、人為的涂畫(比如馬賽克),亦或是圖像本身的損壞。修復(fù)圖像就成為一個(gè)常見(jiàn)的需求了,本文講述Android基于OpenCV實(shí)現(xiàn)圖像修復(fù)的步驟,有此需求的朋友可以參考下2021-06-06