1. 程式人生 > >四種對話方塊(dialog)的簡單使用方法

四種對話方塊(dialog)的簡單使用方法

有普通對話方塊,單選對話方塊,複選對話方塊,進度條的兩種實現方法話不多說,直接上程式碼

activity_main.xml:

 

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     xmlns:tools
="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".MainActivity"> 8 <LinearLayout 9 android:id="@+id/mainLinearLayout" 10 android:layout_width="match_parent" 11 android:layout_height
="match_parent" 12 android:orientation="vertical"> 13 <Button 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:text="普通對話方塊" 17 android:onClick="normalDialog"/> 18 19 <
Button 20 android:layout_width="wrap_content" 21 android:layout_height="wrap_content" 22 android:text="單選對話方塊" 23 android:onClick="singleDialog"/> 24 <Button 25 android:layout_width="wrap_content" 26 android:layout_height="wrap_content" 27 android:text="複選對話方塊" 28 android:onClick="MultiDialog"/> 29 <Button 30 android:layout_width="wrap_content" 31 android:layout_height="wrap_content" 32 android:text="進度條對話方塊" 33 android:onClick="progressDialog"/> 34 <Button 35 android:id="@+id/downlodebutton" 36 android:layout_width="wrap_content" 37 android:layout_height="wrap_content" 38 android:text="進度條對話方塊" 39 android:onClick="progressDialog"/> 40 </LinearLayout> 41 42 </android.support.constraint.ConstraintLayout>

 

MainActivity.java:

 

  1 package com.lgqrlchinese.dialogtext;
  2 
  3 import android.app.ProgressDialog;
  4 import android.content.DialogInterface;
  5 import android.os.SystemClock;
  6 import android.support.v7.app.AlertDialog;
  7 import android.support.v7.app.AppCompatActivity;
  8 import android.os.Bundle;
  9 import android.view.View;
 10 import android.widget.Button;
 11 import android.widget.LinearLayout;
 12 import android.widget.ProgressBar;
 13 import android.widget.Toast;
 14 
 15 
 16 public class MainActivity extends AppCompatActivity {
 17     protected String result;
 18 
 19     @Override
 20     protected void onCreate(Bundle savedInstanceState) {
 21         super.onCreate(savedInstanceState);
 22         setContentView(R.layout.activity_main);
 23         progerssNow();
 24     }
 25 
 26     //普通對話方塊
 27     public void normalDialog(View view) {
 28         //構建器
 29         AlertDialog.Builder b = new AlertDialog.Builder(this);
 30         //設定標題
 31         b.setTitle("普通對話方塊");
 32         //設定提示資訊
 33         b.setMessage("是否確定退出");
 34         //設定圖示
 35         b.setIcon(R.drawable.ic_launcher_background);
 36         //新增確定按鈕
 37         b.setPositiveButton("確定", new DialogInterface.OnClickListener() {
 38             @Override
 39             public void onClick(DialogInterface dialogInterface, int i) {
 40                 finish();
 41             }
 42         });
 43         //新增取消按鈕
 44         b.setNegativeButton("取消", null);
 45         //建立對話方塊
 46         b.create();
 47         //顯示
 48         b.show();
 49     }
 50 
 51     //單選對話方塊
 52     public void singleDialog(View view) {
 53         AlertDialog.Builder b = new AlertDialog.Builder(this);
 54         b.setTitle("單選對話方塊");
 55         //選項內容
 56         final String item[] = {"A", "B", "C", "D"};
 57         //setSingleChoiceItems(顯示的選項內容,預設值,監聽事件)
 58         b.setSingleChoiceItems(item, -1, new DialogInterface.OnClickListener() {
 59             @Override
 60             public void onClick(DialogInterface dialogInterface, int i) {
 61                 //取出陣列中的資料
 62                 result = item[i];
 63                 //吐司顯示出來
 64                 Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
 65             }
 66         });
 67         //設定確定按鈕
 68         b.setPositiveButton("確定", new DialogInterface.OnClickListener() {
 69             @Override
 70             public void onClick(DialogInterface dialogInterface, int i) {
 71                 Toast.makeText(getApplicationContext(), "選擇的結果是" + result, Toast.LENGTH_SHORT).show();
 72             }
 73         });
 74 
 75         b.create();
 76         b.show();
 77     }
 78 
 79     //複選對話方塊
 80     public void MultiDialog(View view) {
 81         AlertDialog.Builder b = new AlertDialog.Builder(this);
 82         b.setTitle("複選框對話方塊");
 83         b.setIcon(R.drawable.ic_launcher_background);
 84         //設定選項
 85         final String item[] = {"A", "B", "C", "D"};
 86         //設定預設值
 87         final boolean chechedItem[] = {true, false, true, true};
 88         //設定多選setMultiChoiceItems(選項陣列,預設值,監聽事件)
 89         b.setMultiChoiceItems(item, chechedItem, new DialogInterface.OnMultiChoiceClickListener() {
 90             @Override
 91             public void onClick(DialogInterface dialogInterface, int i, boolean b) {
 92 
 93             }
 94         });
 95         b.setPositiveButton("確定", new DialogInterface.OnClickListener() {
 96             @Override
 97             public void onClick(DialogInterface dialogInterface, int i) {
 98                 StringBuffer stringBuffer = new StringBuffer();
 99                 for (int i1 = 0; i1 < chechedItem.length; i1++) {
100                     if (chechedItem[i1]) {
101                         stringBuffer.append(item[i1] + "  ");
102                     }
103                 }
104                 Toast.makeText(getApplicationContext(), stringBuffer.toString(), Toast.LENGTH_SHORT).show();
105             }
106         });
107         b.create();
108         b.show();
109     }
110 
111     //進度條對話方塊
112     /*
113      * 在API level 26 中,ProgressDialog被宣告不贊成使用,應使用的替代方法是ProgressBar
114      * */
115     public void progressDialog(View view) {
116         final ProgressDialog progressDialog = new ProgressDialog(this);
117         //設定標題
118         progressDialog.setTitle("正在載入中》》》");
119         //設定樣式
120         progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
121         //(模擬下載)執行緒睡眠
122         new Thread() {
123             @Override
124             public void run() {
125                 //設定最大值
126                 progressDialog.setMax(100);
127                 //設定當前值
128                 for (int i = 0; i < 100; i++) {
129                     progressDialog.setProgress(i);
130                     SystemClock.sleep(500);
131                 }
132                 progressDialog.dismiss();
133             }
134         }.start();
135         progressDialog.show();
136     }
137 
138     //進度條對話方塊(推薦使用)
139     public void progerssNow() {
140         LinearLayout mainLayout;
141         Button downlodebutton;
142         mainLayout = findViewById(R.id.mainLinearLayout);
143         downlodebutton = findViewById(R.id.downlodebutton);
144         final ProgressBar progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
145         downlodebutton.setOnClickListener(new View.OnClickListener() {
146             @Override
147             public void onClick(View view) {
148                 new Thread() {
149                     @Override
150                     public void run() {
151 
152                         progressBar.setMax(100);
153                         for (int i = 0; i < 100; i++) {
154                             progressBar.setProgress(i);
155                             SystemClock.sleep(12);
156                         }
157 
158                     }
159                 }.start();
160             }
161         });
162         mainLayout.addView(progressBar);
163     }
164 }