Android中常用的三個Dialog彈窗總結(jié)解析
ProgressDialog
private void showProgressDialog(){
progressDialog = new ProgressDialog(DialogDemo.this);
//設(shè)置提示信息
progressDialog.setTitle("提示");
progressDialog.setIcon(R.mipmap.touxiang0);
progressDialog.setMessage("正在處理中");
//是否用過返回鍵取消
progressDialog.setCancelable(true);
//碰觸彈框之外的地方取消
progressDialog.setCanceledOnTouchOutside(true);
//顯示
progressDialog.show();
}

DatePickerDialog
//日期
private void datePickerDialog(){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
DatePickerDialog datePickerDialog = new DatePickerDialog(DialogDemo.this);
datePickerDialog.setOnDateSetListener(new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
Toast.makeText(DialogDemo.this,year+"年"+(month+1)+"月"+dayOfMonth+"日",Toast.LENGTH_SHORT).show();
}
});
datePickerDialog.show();
}else {
Toast.makeText(DialogDemo.this,"版本過低",Toast.LENGTH_SHORT).show();
}
}

TimePickerDialog
//時間
private void timePickerDialog(){
//獲得日歷的實列
Calendar calendar = Calendar.getInstance();
//設(shè)置當前時間
calendar.setTimeInMillis(System.currentTimeMillis());
//獲取時分
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
//第三、四個參數(shù)初始時分 第五個參數(shù)是否為24小時顯示
TimePickerDialog time = new TimePickerDialog(DialogDemo.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Toast.makeText(DialogDemo.this,"Hour"+hourOfDay+"minute"+minute,Toast.LENGTH_SHORT).show();
}
},hour,minute,true);
time.show();
}

布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:gravity="center_horizontal"
>
<Button
android:id="@+id/btnProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="35dp"
android:backgroundTint="#64D7E6"
android:text="提示"
android:textSize="35sp"
/>
<Button
android:id="@+id/btnDatePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="35dp"
android:backgroundTint="#64D7E6"
android:text="日期"
android:textSize="35sp"
/>
<Button
android:id="@+id/btnTimePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="35dp"
android:backgroundTint="#64D7E6"
android:text="時間"
android:textSize="35sp"
/>
</LinearLayout>
完整代碼
import androidx.appcompat.app.AppCompatActivity;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;
import java.util.Calendar;
public class DialogDemo extends AppCompatActivity {
Button mBtnProgress,mBtnDatePicker,mBtnTimePicker;
ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog_demo);
initView();
MyOnClick myOnClick = new MyOnClick();
mBtnProgress.setOnClickListener(myOnClick);
mBtnDatePicker.setOnClickListener(myOnClick);
mBtnTimePicker.setOnClickListener(myOnClick);
}
private void initView(){
mBtnProgress = findViewById(R.id.btnProgress);
mBtnDatePicker = findViewById(R.id.btnDatePicker);
mBtnTimePicker = findViewById(R.id.btnTimePicker);
}
class MyOnClick implements View.OnClickListener {
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btnProgress:
showProgressDialog();
break;
case R.id.btnDatePicker:
datePickerDialog();
break;
case R.id.btnTimePicker:
timePickerDialog();
break;
}
}
}
private void showProgressDialog(){
progressDialog = new ProgressDialog(DialogDemo.this);
//設(shè)置提示信息
progressDialog.setTitle("提示");
progressDialog.setIcon(R.mipmap.touxiang0);
progressDialog.setMessage("正在處理中");
//是否用過返回鍵取消
progressDialog.setCancelable(true);
//碰觸彈框之外的地方取消
progressDialog.setCanceledOnTouchOutside(true);
//顯示
progressDialog.show();
}
//日期
private void datePickerDialog(){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
DatePickerDialog datePickerDialog = new DatePickerDialog(DialogDemo.this);
datePickerDialog.setOnDateSetListener(new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
Toast.makeText(DialogDemo.this,year+"年"+(month+1)+"月"+dayOfMonth+"日",Toast.LENGTH_SHORT).show();
}
});
datePickerDialog.show();
}else {
Toast.makeText(DialogDemo.this,"版本過低",Toast.LENGTH_SHORT).show();
}
}
//時間
private void timePickerDialog(){
//獲得日歷的實列
Calendar calendar = Calendar.getInstance();
//設(shè)置當前時間
calendar.setTimeInMillis(System.currentTimeMillis());
//獲取時分
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
//第三、四個參數(shù)初始時分 第五個參數(shù)是否為24小時顯示
TimePickerDialog time = new TimePickerDialog(DialogDemo.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Toast.makeText(DialogDemo.this,"Hour"+hourOfDay+"minute"+minute,Toast.LENGTH_SHORT).show();
}
},hour,minute,true);
time.show();
}
}
到此這篇關(guān)于Android中常用的三個Dialog彈窗總結(jié)解析的文章就介紹到這了,更多相關(guān)Android Dialog內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android開發(fā)導入項目報錯Ignoring InnerClasses attribute for an anonym
今天小編就為大家分享一篇關(guān)于Android開發(fā)導入項目報錯Ignoring InnerClasses attribute for an anonymous inner class的解決辦法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12
Android Studio 3.6中使用視圖綁定替代 findViewById的方法
從 Android Studio 3.6 開始,視圖綁定能夠通過生成綁定對象來替代 findViewById,從而可以幫您簡化代碼、移除 bug,并且從 findViewById 的模版代碼中解脫出來,今天通過本文給大家介紹使用視圖綁定替代 findViewById的方法,感興趣的朋友一起看看吧2020-03-03
Android實現(xiàn)在一個activity中添加多個listview的方法
這篇文章主要介紹了Android實現(xiàn)在一個activity中添加多個listview的方法,分析了Activity中添加listview的原理與具體實現(xiàn)方法,需要的朋友可以參考下2016-08-08
android中實現(xiàn)完全退出程序方法(退出所有activity)
這篇文章主要介紹了android中實現(xiàn)完全退出程序方法(退出所有activity),本文方法是博主個人使用的一個方法,據(jù)說效果非常好,需要的朋友可以參考下2015-05-05
Android MaterialCardView的使用介紹與示例
MaterialCardView是一個基于Android支持庫中的CardView的可自定義組件。 MaterialCardView提供了CardView的所有功能,但增加了一些自定義屬性,使用起來更加方便實用2021-11-11

