Android中常用的三個(gè)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
//時(shí)間 private void timePickerDialog(){ //獲得日歷的實(shí)列 Calendar calendar = Calendar.getInstance(); //設(shè)置當(dāng)前時(shí)間 calendar.setTimeInMillis(System.currentTimeMillis()); //獲取時(shí)分 int hour = calendar.get(Calendar.HOUR_OF_DAY); int minute = calendar.get(Calendar.MINUTE); //第三、四個(gè)參數(shù)初始時(shí)分 第五個(gè)參數(shù)是否為24小時(shí)顯示 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="時(shí)間" 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(); } } //時(shí)間 private void timePickerDialog(){ //獲得日歷的實(shí)列 Calendar calendar = Calendar.getInstance(); //設(shè)置當(dāng)前時(shí)間 calendar.setTimeInMillis(System.currentTimeMillis()); //獲取時(shí)分 int hour = calendar.get(Calendar.HOUR_OF_DAY); int minute = calendar.get(Calendar.MINUTE); //第三、四個(gè)參數(shù)初始時(shí)分 第五個(gè)參數(shù)是否為24小時(shí)顯示 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中常用的三個(gè)Dialog彈窗總結(jié)解析的文章就介紹到這了,更多相關(guān)Android Dialog內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android開發(fā)導(dǎo)入項(xiàng)目報(bào)錯(cuò)Ignoring InnerClasses attribute for an anonym
今天小編就為大家分享一篇關(guān)于Android開發(fā)導(dǎo)入項(xiàng)目報(bào)錯(cuò)Ignoring InnerClasses attribute for an anonymous inner class的解決辦法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12Android實(shí)現(xiàn)日夜間模式的深入理解
相信Android的日間/夜間模式切換相信大家在平時(shí)使用 APP 的過程中都遇到過,比如知乎、簡書中就有相關(guān)的模式切換。實(shí)現(xiàn)日間/夜間模式切換的方案也有許多種,趁著今天有空來講一下日間/夜間模式切換的幾種實(shí)現(xiàn)方案,也可以做一個(gè)橫向的對(duì)比來看看哪種方案最好。2016-09-09Android Studio 3.6中使用視圖綁定替代 findViewById的方法
從 Android Studio 3.6 開始,視圖綁定能夠通過生成綁定對(duì)象來替代 findViewById,從而可以幫您簡化代碼、移除 bug,并且從 findViewById 的模版代碼中解脫出來,今天通過本文給大家介紹使用視圖綁定替代 findViewById的方法,感興趣的朋友一起看看吧2020-03-03Android實(shí)現(xiàn)在一個(gè)activity中添加多個(gè)listview的方法
這篇文章主要介紹了Android實(shí)現(xiàn)在一個(gè)activity中添加多個(gè)listview的方法,分析了Activity中添加listview的原理與具體實(shí)現(xiàn)方法,需要的朋友可以參考下2016-08-08android中實(shí)現(xiàn)完全退出程序方法(退出所有activity)
這篇文章主要介紹了android中實(shí)現(xiàn)完全退出程序方法(退出所有activity),本文方法是博主個(gè)人使用的一個(gè)方法,據(jù)說效果非常好,需要的朋友可以參考下2015-05-05Android MaterialCardView的使用介紹與示例
MaterialCardView是一個(gè)基于Android支持庫中的CardView的可自定義組件。 MaterialCardView提供了CardView的所有功能,但增加了一些自定義屬性,使用起來更加方便實(shí)用2021-11-11