Android時間選擇器、日期選擇器實現(xiàn)代碼
本文為大家分享了兩款選擇器,一款可以針對時間進(jìn)行選擇、一款可以針對日期進(jìn)行選擇,供大家參考,具體內(nèi)容如下
一、時間選擇器
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="時間" 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指的是時間,true表示是否為24小時,true為24小時制 },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日歷選擇器的對話框,監(jiān)聽為OnDateSetListener(){..}
效果圖:
以上就是兩款A(yù)ndroid時間選擇器、Android日期選擇器的實現(xiàn)代碼,希望對大家學(xué)習(xí)Android軟件編程有所幫助。
相關(guān)文章
Android中使用CircleImageView和Cardview制作圓形頭像的方法
這篇文章主要介紹了Android中使用CircleImageView和Cardview制作圓形頭像的方法,簡單介紹了CircleImageView和Cardview的使用,需要的朋友可以參考下2016-09-09Android studio配置國內(nèi)鏡像源的實現(xiàn)
這篇文章主要介紹了Android studio配置國內(nèi)鏡像源的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11Android實現(xiàn)向Launcher添加快捷方式的方法
這篇文章主要介紹了Android實現(xiàn)向Launcher添加快捷方式的方法,涉及Android添加快捷方式的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-09-09Android RecyclerView實現(xiàn)吸頂動態(tài)效果流程分析
RecyclerView是Android一個更強(qiáng)大的控件,其不僅可以實現(xiàn)和ListView同樣的效果,還有優(yōu)化了ListView中的各種不足。其可以實現(xiàn)數(shù)據(jù)縱向滾動,也可以實現(xiàn)橫向滾動(ListView做不到橫向滾動)。接下來講解RecyclerView的用法2022-12-12Pagerslidingtabstrip菜單標(biāo)題欄制作方法
這篇文章主要為大家詳細(xì)介紹了Pagerslidingtabstrip菜單標(biāo)題欄的制作方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10Android基于OpenCV實現(xiàn)圖像修復(fù)
實際應(yīng)用中,圖像常常容易受損,如存在污漬的鏡頭、舊照片的劃痕、人為的涂畫(比如馬賽克),亦或是圖像本身的損壞。修復(fù)圖像就成為一個常見的需求了,本文講述Android基于OpenCV實現(xiàn)圖像修復(fù)的步驟,有此需求的朋友可以參考下2021-06-06