Android對話框AlertDialog與DatePickerDialog及TimePickerDialog使用詳解
一、提醒對話框AlertDialog
AlertDialog可以完成常見的交互操作,如提示、確認、選擇等功能,AlertDialog借助建造器AlertDialog.Builder才能完成參數(shù)設置。
調用建造器的create方法生成對話框實例,再調用對話框實例的show方法,在頁面上彈出提醒對話框。
AlterDialog.Builder的常用方法說明:
- setIcon:設置對話框的標題圖標。
- setTitle:設置對話框的標題文本。
- setMessage:設置對話框的內容文本。
- setPositiveButton:設置肯定按鈕的信息,包括文本和監(jiān)聽器。
- setNegativeButton:設置否定按鈕的信息,包括文本和監(jiān)聽器。
- setNeutralButton:設置中性按鈕的信息,包括文本和監(jiān)聽器。(比較少用)
例:彈出卸載對話框
XML文件
<Button android:id="@+id/btn_alert" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="彈出提醒對話框"/> <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)建提醒對話框的建造器 AlertDialog.Builder builder = new AlertDialog.Builder(this); //設置對話框的標題文本 builder.setTitle("尊敬的用戶"); //設置對話的內容文本 builder.setMessage("確定卸載?"); //設置對話框的肯定按鈕文本及其監(jiān)聽器 builder.setPositiveButton("卸載",(dialog,which) -> { tv_alert.setText("再見"); }); builder.setNegativeButton("再想想",(dialog,which) -> { tv_alert.setText("留下來"); }); //根據(jù)建造器構建對話框 AlertDialog dialog = builder.create(); dialog.show(); } }
二、日期對話框DatePickerDialog
日期選擇器DatePicker可以讓用戶選擇具體的年月日。
但DatePicker并非彈窗模式,而是在當前頁面占據(jù)一塊區(qū)域,并且不會自動關閉。
DatePickerDialog相當于在AlertDialog上裝載了DatePicker,日期選擇事件則由監(jiān)聽器OnDateSetListener負責響應,在該監(jiān)聽器的onDateSet方法中,開發(fā)者獲取用戶選擇的具體日期再做后續(xù)處理。
第一種-點擊選擇日期出現(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="請選擇日期"/> <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: //獲取日歷實例,里面包含了當前的年月日 Calendar calendar = Calendar.getInstance(); DatePickerDialog dialog = new DatePickerDialog(this,this,calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)); //顯示日期對話框 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); } }
第二種-滾動選擇日期
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; } } }
三、時間對話框TimePickerDialog
時間選擇器TimePicker可以讓用戶選擇具體的小時和分鐘。
TimePickerDialog用法類似DatePickerDialog。
方式一-出現(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_time" 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, 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(); //構建時間話對話框 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時%d分",hourOfDay,minute); tv_time.setText(desc); } }
方式二-滾動選擇時間
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時%d分",tp_time.getCurrentHour(),tp_time.getCurrentMinute()); tv_time.setText(desc); break; } } }
到此這篇關于Android對話框AlertDialog與DatePickerDialog及TimePickerDialog使用詳解的文章就介紹到這了,更多相關Android AlertDialog內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
android之計時器(Chronometer)的使用以及常用的方法
在Android的SDK中,為我們提供了一個計時器,這個計時器稱為Chronometer,我們可以成它為Android的一個組件,同時它也具備自己獨有的方法2013-01-01Android中的webview支持頁面中的文件上傳實例代碼
本篇文章主要介紹了Android中的webview支持頁面中的文件上傳,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03