網(wǎng)站首頁 編程語言 正文
Android對話框AlertDialog與DatePickerDialog及TimePickerDialog使用詳解_Android
作者:Shewyoo ? 更新時間: 2022-11-17 編程語言一、提醒對話框AlertDialog
AlertDialog可以完成常見的交互操作,如提示、確認(rèn)、選擇等功能,AlertDialog借助建造器AlertDialog.Builder才能完成參數(shù)設(shè)置。
調(diào)用建造器的create方法生成對話框?qū)嵗僬{(diào)用對話框?qū)嵗膕how方法,在頁面上彈出提醒對話框。
AlterDialog.Builder的常用方法說明:
- setIcon:設(shè)置對話框的標(biāo)題圖標(biāo)。
- setTitle:設(shè)置對話框的標(biāo)題文本。
- setMessage:設(shè)置對話框的內(nèi)容文本。
- setPositiveButton:設(shè)置肯定按鈕的信息,包括文本和監(jiān)聽器。
- setNegativeButton:設(shè)置否定按鈕的信息,包括文本和監(jiān)聽器。
- setNeutralButton:設(shè)置中性按鈕的信息,包括文本和監(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);
//設(shè)置對話框的標(biāo)題文本
builder.setTitle("尊敬的用戶");
//設(shè)置對話的內(nèi)容文本
builder.setMessage("確定卸載?");
//設(shè)置對話框的肯定按鈕文本及其監(jiān)聽器
builder.setPositiveButton("卸載",(dialog,which) -> {
tv_alert.setText("再見");
});
builder.setNegativeButton("再想想",(dialog,which) -> {
tv_alert.setText("留下來");
});
//根據(jù)建造器構(gòu)建對話框
AlertDialog dialog = builder.create();
dialog.show();
}
}
二、日期對話框DatePickerDialog
日期選擇器DatePicker可以讓用戶選擇具體的年月日。
但DatePicker并非彈窗模式,而是在當(dāng)前頁面占據(jù)一塊區(qū)域,并且不會自動關(guān)閉。
DatePickerDialog相當(dāng)于在AlertDialog上裝載了DatePicker,日期選擇事件則由監(jiān)聽器OnDateSetListener負(fù)責(zé)響應(yīng),在該監(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:
//獲取日歷實例,里面包含了當(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));
//顯示日期對話框
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();
//構(gòu)建時間話對話框
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;
}
}
}
原文鏈接:https://blog.csdn.net/Tir_zhang/article/details/127014142
相關(guān)推薦
- 2022-05-13 Linux操作系統(tǒng)筆記——GCC編譯器
- 2023-11-20 Linux、jetson nano、JTX、英偉達(dá)、nVidia查看cuda版本
- 2022-10-05 Iptables防火墻string模塊擴展匹配規(guī)則_安全相關(guān)
- 2022-01-29 寶塔部署Yii框架多個商城項目,隊列問題“服務(wù)測試失敗,請檢查服務(wù)是否正常運行”
- 2022-08-06 C語言結(jié)構(gòu)體數(shù)組常用的三種賦值方法(包含字符串)_C 語言
- 2022-04-25 Oracle導(dǎo)出導(dǎo)入表結(jié)構(gòu)操作實戰(zhàn)記錄_oracle
- 2022-07-14 使用react-activation實現(xiàn)keepAlive支持返回傳參_React
- 2022-11-21 C++運行時類型識別與轉(zhuǎn)換實現(xiàn)方法_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支