Android對(duì)話框AlertDialog與DatePickerDialog及TimePickerDialog使用詳解
一、提醒對(duì)話框AlertDialog
AlertDialog可以完成常見(jiàn)的交互操作,如提示、確認(rèn)、選擇等功能,AlertDialog借助建造器AlertDialog.Builder才能完成參數(shù)設(shè)置。
調(diào)用建造器的create方法生成對(duì)話框?qū)嵗?,再調(diào)用對(duì)話框?qū)嵗膕how方法,在頁(yè)面上彈出提醒對(duì)話框。
AlterDialog.Builder的常用方法說(shuō)明:
- setIcon:設(shè)置對(duì)話框的標(biāo)題圖標(biāo)。
- setTitle:設(shè)置對(duì)話框的標(biāo)題文本。
- setMessage:設(shè)置對(duì)話框的內(nèi)容文本。
- setPositiveButton:設(shè)置肯定按鈕的信息,包括文本和監(jiān)聽(tīng)器。
- setNegativeButton:設(shè)置否定按鈕的信息,包括文本和監(jiān)聽(tīng)器。
- setNeutralButton:設(shè)置中性按鈕的信息,包括文本和監(jiān)聽(tīng)器。(比較少用)
例:彈出卸載對(duì)話框
XML文件
<Button android:id="@+id/btn_alert" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="彈出提醒對(duì)話框"/> <TextView android:id="@+id/tv_alert" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp"/>
java代碼
public class AlertDialogActivity extends AppCompatActivity implements View.OnClickListener { private TextView tv_alert; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_alert_dialog); findViewById(R.id.btn_alert).setOnClickListener(this); tv_alert = findViewById(R.id.tv_alert); } @Override public void onClick(View view) { //創(chuàng)建提醒對(duì)話框的建造器 AlertDialog.Builder builder = new AlertDialog.Builder(this); //設(shè)置對(duì)話框的標(biāo)題文本 builder.setTitle("尊敬的用戶"); //設(shè)置對(duì)話的內(nèi)容文本 builder.setMessage("確定卸載?"); //設(shè)置對(duì)話框的肯定按鈕文本及其監(jiān)聽(tīng)器 builder.setPositiveButton("卸載",(dialog,which) -> { tv_alert.setText("再見(jiàn)"); }); builder.setNegativeButton("再想想",(dialog,which) -> { tv_alert.setText("留下來(lái)"); }); //根據(jù)建造器構(gòu)建對(duì)話框 AlertDialog dialog = builder.create(); dialog.show(); } }
二、日期對(duì)話框DatePickerDialog
日期選擇器DatePicker可以讓用戶選擇具體的年月日。
但DatePicker并非彈窗模式,而是在當(dāng)前頁(yè)面占據(jù)一塊區(qū)域,并且不會(huì)自動(dòng)關(guān)閉。
DatePickerDialog相當(dāng)于在AlertDialog上裝載了DatePicker,日期選擇事件則由監(jiān)聽(tīng)器OnDateSetListener負(fù)責(zé)響應(yīng),在該監(jiān)聽(tīng)器的onDateSet方法中,開(kāi)發(fā)者獲取用戶選擇的具體日期再做后續(xù)處理。
第一種-點(diǎn)擊選擇日期出現(xiàn)日歷
XML文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="5dp"> <Button android:id="@+id/btn_date" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="請(qǐng)選擇日期"/> <TextView android:id="@+id/tv_date" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>
java代碼
public class DatePickerActivity extends AppCompatActivity implements View.OnClickListener, DatePickerDialog.OnDateSetListener { private DatePicker dp_date; private TextView tv_date; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_date_picker); findViewById(R.id.btn_date).setOnClickListener(this); tv_date = findViewById(R.id.tv_date); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn_date: //獲取日歷實(shí)例,里面包含了當(dāng)前的年月日 Calendar calendar = Calendar.getInstance(); DatePickerDialog dialog = new DatePickerDialog(this,this,calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)); //顯示日期對(duì)話框 dialog.show(); break; } } @Override public void onDateSet(DatePicker datePicker, int year, int month, int dayOfMonth) { String desc = String.format("您選擇的日期是%d年%d月%d日",year,month+1,dayOfMonth); tv_date.setText(desc); } }
第二種-滾動(dòng)選擇日期
XML文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="5dp"> <DatePicker android:id="@+id/dp_date" android:layout_width="match_parent" android:layout_height="wrap_content" android:datePickerMode="spinner" android:calendarViewShown="false"/> <Button android:id="@+id/btn_ok" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="確定"/> <TextView android:id="@+id/tv_date" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>
java代碼
public class DatePickerActivity extends AppCompatActivity implements View.OnClickListener { private DatePicker dp_date; private TextView tv_date; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_date_picker); findViewById(R.id.btn_ok).setOnClickListener(this); tv_date = findViewById(R.id.tv_date); dp_date = findViewById(R.id.dp_date); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn_ok: String desc = String.format("您選擇的日期是%d年%d月%d日",dp_date.getYear(),dp_date.getMonth()+1,dp_date.getDayOfMonth()); tv_date.setText(desc); break; } } }
三、時(shí)間對(duì)話框TimePickerDialog
時(shí)間選擇器TimePicker可以讓用戶選擇具體的小時(shí)和分鐘。
TimePickerDialog用法類似DatePickerDialog。
方式一-出現(xiàn)時(shí)鐘選擇時(shí)間
XML文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="5dp"> <Button android:id="@+id/btn_time" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="請(qǐng)選擇時(shí)間"/> <TextView android:id="@+id/tv_time" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>
java代碼
public class TimePickerActivity extends AppCompatActivity implements View.OnClickListener, TimePickerDialog.OnTimeSetListener { private TextView tv_time; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_time_picker); findViewById(R.id.btn_time).setOnClickListener(this); tv_time = findViewById(R.id.tv_time); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.btn_time: //獲取日歷 Calendar calendar = Calendar.getInstance(); //構(gòu)建時(shí)間話對(duì)話框 TimePickerDialog dialog = new TimePickerDialog(this,this,calendar.get(Calendar.HOUR_OF_DAY),calendar.get(Calendar.MINUTE),true); dialog.show(); break; } } @Override public void onTimeSet(TimePicker timePicker, int hourOfDay, int minute) { String desc = String.format("您選擇的日期是%d時(shí)%d分",hourOfDay,minute); tv_time.setText(desc); } }
方式二-滾動(dòng)選擇時(shí)間
XML文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="5dp"> <TimePicker android:id="@+id/tp_time" android:layout_width="match_parent" android:layout_height="wrap_content" android:timePickerMode="spinner"/> <Button android:id="@+id/btn_ok" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="確定"/> <TextView android:id="@+id/tv_time" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>
java代碼
public class TimePickerActivity extends AppCompatActivity implements View.OnClickListener{ private TimePicker tp_time; private TextView tv_time; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_time_picker); findViewById(R.id.btn_ok).setOnClickListener(this); tp_time = findViewById(R.id.tp_time); tp_time.setIs24HourView(true); tv_time = findViewById(R.id.tv_time); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.btn_ok: String desc = String.format("您選擇的日期是%d時(shí)%d分",tp_time.getCurrentHour(),tp_time.getCurrentMinute()); tv_time.setText(desc); break; } } }
到此這篇關(guān)于Android對(duì)話框AlertDialog與DatePickerDialog及TimePickerDialog使用詳解的文章就介紹到這了,更多相關(guān)Android AlertDialog內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android對(duì)話框AlertDialog詳解
- Android AlertDialog(對(duì)話框)實(shí)例詳解
- Android AlertDialog六種創(chuàng)建方式案例詳解
- Android AlertDialog多種創(chuàng)建方式案例詳解
- Android AlertDialog的幾種用法詳解
- Android開(kāi)發(fā)之DatePickerDialog、TimePickerDialog時(shí)間日期對(duì)話框用法示例
- Android使用DatePickerDialog顯示時(shí)間
- Android時(shí)間對(duì)話框TimePickerDialog詳解
相關(guān)文章
Android軟鍵盤(pán)顯示模式及打開(kāi)和關(guān)閉方式(推薦)
這篇文章主要介紹了Android軟鍵盤(pán)顯示模式及打開(kāi)和關(guān)閉方式(推薦),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02Android如何動(dòng)態(tài)改變App桌面圖標(biāo)
這篇文章主要介紹了 Android動(dòng)態(tài)改變App桌面圖標(biāo)的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-01-01android之計(jì)時(shí)器(Chronometer)的使用以及常用的方法
在Android的SDK中,為我們提供了一個(gè)計(jì)時(shí)器,這個(gè)計(jì)時(shí)器稱為Chronometer,我們可以成它為Android的一個(gè)組件,同時(shí)它也具備自己獨(dú)有的方法2013-01-01Android中的webview支持頁(yè)面中的文件上傳實(shí)例代碼
本篇文章主要介紹了Android中的webview支持頁(yè)面中的文件上傳,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03Android?Choreographer源碼詳細(xì)分析
Choreographer的作用主要是配合Vsync,給上層App的渲染提供一個(gè)穩(wěn)定的Message處理的時(shí)機(jī),也就是Vsync到來(lái)的時(shí)候,系統(tǒng)通過(guò)對(duì)Vsync信號(hào)周期的調(diào)整,來(lái)控制每一幀繪制操作的時(shí)機(jī)2022-08-08Android 幀動(dòng)畫(huà)的實(shí)例詳解
這篇文章主要介紹了Android 幀動(dòng)畫(huà)的實(shí)例詳解的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家掌握這部分內(nèi)容,需要的朋友可以參考下2017-10-10Flutter網(wǎng)絡(luò)請(qǐng)求Dio庫(kù)的使用及封裝詳解
本文主要介紹了Flutter網(wǎng)絡(luò)請(qǐng)求Dio庫(kù)的使用及封裝詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04