場景
Android中常用的五種對話框為
常規對話框、帶列表的對話框、自定義的對話框、帶進度條的對話框、帶日期選擇器的對話框。
注:
博客:
https://blog.csdn.net/badao_liumang_qizhi
關注公眾號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。
實現
常規對話框
新建一個空項目,調整activity_main.xml的布局為LinearLayout,並添加一個Button,設置其點擊事件
<Button android:id="@+id/button_normal_dialog" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="startNormalDialog" android:text="啟動常規對話框" />
然后在activity中實現其點擊方法
/*** * 啟動常規對話框 * @param view */ public void startNormalDialog(View view){ new AlertDialog.Builder(this) .setIcon(R.drawable.ic_launcher_background) .setTitle("公眾號:霸道的程序猿") .setCancelable(false) .setMessage("是否刪除該條記錄") .setPositiveButton("是", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"刪除記錄成功",Toast.LENGTH_LONG).show(); } }) .setNegativeButton("否", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"取消刪除記錄",Toast.LENGTH_LONG).show(); } }) .show(); }
運行項目查看效果
帶列表的對話框
在布局文件中添加一個按鈕
<Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="startListDialog" android:text="啟動帶列表的對話框" />
在activity中實現其點擊方法
/** * 啟動帶列表的對話框 * @param view */ public void startListDialog(View view){ final String[] items = {"關注","公眾號","霸道的程序猿","獲取","編程教程與資源"}; new AlertDialog.Builder(this) .setTitle("公眾號:霸道的程序猿") .setCancelable(false) .setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,items[which],Toast.LENGTH_LONG).show(); } }) .setPositiveButton("OK",null) .show(); }
運行之后查看效果
自定義視圖的對話框
在layout下新建layout資源文件dialog_view.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation= "vertical"> <EditText android:id="@+id/text_username" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:hint="用戶名" android:inputType="textPersonName" /> <EditText android:id="@+id/text_pwd" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:hint="密碼" android:inputType="textPersonName" /> </androidx.appcompat.widget.LinearLayoutCompat>
然后在主布局文件中添加一個按鈕
<Button android:id="@+id/button3" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="startCustomDialog" android:text="啟動自定義的對話框" />
在activity中實現其點擊方法
/** * 啟動自定義的對話框 * @param view */ public void startCustomDialog(View view){ View v = View.inflate(this,R.layout.dialog_view,null); final EditText user = v.findViewById(R.id.text_username); final EditText pwd = v.findViewById(R.id.text_pwd); new AlertDialog.Builder(this) .setTitle("公眾號:霸道的程序猿") .setCancelable(false) .setView(v) .setPositiveButton("登錄", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String username = user.getText().toString().trim(); String password = pwd.getText().toString().trim(); Toast.makeText(MainActivity.this,"用戶名:"+username+"密碼:"+password,Toast.LENGTH_LONG).show(); } }) .show(); }
運行之后查看效果
帶進度條的對話框
在布局文件中添加按鈕
<Button android:id="@+id/button4" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="startProgressDialog" android:text="啟動帶進度條的對話框" />
在activity中實現其點擊方法
/** * 啟動帶進度條的對話框 * @param view */ public void startProgressDialog(View view){ final ProgressDialog progressDialog = new ProgressDialog(this); progressDialog.setTitle("公眾號:霸道的程序猿"); progressDialog.setCancelable(false); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.show(); new Thread(new Runnable() { @Override public void run() { for(inti=1;i<=20;i++){ try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } progressDialog.setProgress(progressDialog.getProgress()+5); } progressDialog.dismiss(); } }).start(); }
運行之后查看效果
帶日期選擇器的對話框
在布局文件中添加按鈕
<Button android:id="@+id/button5" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="startDatePickDialog" android:text="啟動帶日期選擇器的對話框" />
在activity中實現其點擊方法
/** * 啟動帶日期選擇器的對話框 * @param view */ public void startDatePickDialog(View view){ Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DAY_OF_MONTH); new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { Toast.makeText(MainActivity.this,"選中了"+year+"年"+month+"月"+dayOfMonth+"日",Toast.LENGTH_LONG).show(); } },year,month,day).show(); }
運行之后查看效果
activity_main.xml完整代碼
<?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" android:orientation="vertical" tools:context=".MainActivity"> <Button android:id="@+id/button_normal_dialog" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="startNormalDialog" android:text="啟動常規對話框" /> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="startListDialog" android:text="啟動帶列表的對話框" /> <Button android:id="@+id/button3" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="startCustomDialog" android:text="啟動自定義的對話框" /> <Button android:id="@+id/button4" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="startProgressDialog" android:text="啟動帶進度條的對話框" /> <Button android:id="@+id/button5" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="startDatePickDialog" android:text="啟動帶日期選擇器的對話框" /> </LinearLayout>
MainActivity完整代碼
package com.badao.androidstudy; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import android.app.DatePickerDialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.DatePicker; import android.widget.EditText; import android.widget.Toast; import java.util.Calendar; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } /*** * 啟動常規對話框 * @param view */ public void startNormalDialog(View view){ new AlertDialog.Builder(this) .setIcon(R.drawable.ic_launcher_background) .setTitle("公眾號:霸道的程序猿") .setCancelable(false) .setMessage("是否刪除該條記錄") .setPositiveButton("是", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"刪除記錄成功",Toast.LENGTH_LONG).show(); } }) .setNegativeButton("否", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"取消刪除記錄",Toast.LENGTH_LONG).show(); } }) .show(); } /** * 啟動帶列表的對話框 * @param view */ public void startListDialog(View view){ final String[] items = {"關注","公眾號","霸道的程序猿","獲取","編程教程與資源"}; new AlertDialog.Builder(this) .setTitle("公眾號:霸道的程序猿") .setCancelable(false) .setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,items[which],Toast.LENGTH_LONG).show(); } }) .setPositiveButton("OK",null) .show(); } /** * 啟動自定義的對話框 * @param view */ public void startCustomDialog(View view){ View v = View.inflate(this,R.layout.dialog_view,null); final EditText user = v.findViewById(R.id.text_username); final EditText pwd = v.findViewById(R.id.text_pwd); new AlertDialog.Builder(this) .setTitle("公眾號:霸道的程序猿") .setCancelable(false) .setView(v) .setPositiveButton("登錄", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String username = user.getText().toString().trim(); String password = pwd.getText().toString().trim(); Toast.makeText(MainActivity.this,"用戶名:"+username+"密碼:"+password,Toast.LENGTH_LONG).show(); } }) .show(); } /** * 啟動帶進度條的對話框 * @param view */ public void startProgressDialog(View view){ final ProgressDialog progressDialog = new ProgressDialog(this); progressDialog.setTitle("公眾號:霸道的程序猿"); progressDialog.setCancelable(false); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.show(); new Thread(new Runnable() { @Override public void run() { for(int i=1;i<=20;i++){ try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } progressDialog.setProgress(progressDialog.getProgress()+5); } progressDialog.dismiss(); } }).start(); } /** * 啟動帶日期選擇器的對話框 * @param view */ public void startDatePickDialog(View view){ Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DAY_OF_MONTH); new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { Toast.makeText(MainActivity.this,"選中了"+year+"年"+month+"月"+dayOfMonth+"日",Toast.LENGTH_LONG).show(); } },year,month,day).show(); } }