Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 對話框 Dialog 全面詳解

編輯:關於 Android 程式設計

目錄:
1. Dialog 概述
2. Dialog 中的重要角色
3. 常見幾種類型 Dialog 的使用
3.1 提示型 Dialog
3.2 List 型 Dialog
3.3 單選型 Dialog
3.4 多選型 Dialog
3.5 自定義 Dialog

1. Dialog 概述

Dialog 是一個用於向用戶反饋操作的彈窗,通常用來做一些簡單提示或讓用戶做簡單選擇。

2. Dialog 中的重要角色

2.1 AlertDialog 類:這是我們實際使用的窗口類,可通過它的相關方法設置 Dialog 的屬性並顯示。由於它的構造方法都是受保護的,通常我們通過其內部類 Builder 來構建實例。

2.2 AlertDialog.Builder 類:它是 AlertDialog 的內部類,用來實例化 Dialog,同時也能設置 AlertDialog 的屬性資源並負責顯示。

3. 常見幾種類型 Dialog 的使用

3.1 MainActivity.java

 package com.example.dialog;
 import android.content.DialogInterface;
 import android.support.v7.app.AlertDialog;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.Toast;
 public class MainActivity extends AppCompatActivity implements View.OnClickListener {
     private Button simpleButton, listButton, singleButton, multiButton, customButton;
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         // 初始化控件
         simpleButton = (Button) findViewById(R.id.info_dg);
         listButton = (Button) findViewById(R.id.list_dg);
         singleButton = (Button) findViewById(R.id.single_dg);
         multiButton = (Button) findViewById(R.id.multi_dg);
         customButton = (Button) findViewById(R.id.custom_dg);
         // 設置監聽器
         simpleButton.setOnClickListener(this);
         listButton.setOnClickListener(this);
         singleButton.setOnClickListener(this);
         multiButton.setOnClickListener(this);
         customButton.setOnClickListener(this);
     }
     /* 彈出多選型彈框 */
     public void showMultiDialog() {
         final CharSequence[] items = {"籃球", "足球", "游泳", "唱歌"};
         final StringBuilder checkedStr = new StringBuilder();
         AlertDialog.Builder builder = new AlertDialog.Builder(this);
         builder.setTitle("多選彈框標題");
         builder.setIcon(R.drawable.a12);
         builder.setMultiChoiceItems(items, null, new DialogInterface.OnMultiChoiceClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                 if (isChecked) {
                     checkedStr.append(items[which].toString());
                 }
             }
         });
         builder.setPositiveButton("確認", new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int which) {
                 Toast.makeText(MainActivity.this, checkedStr, Toast.LENGTH_SHORT).show();
             }
         });
         builder.show();
     }
     /* 彈出單選型彈框 */
     public void showSingleDialog() {
         final CharSequence[] items = {"男", "女", "保密"};
         AlertDialog.Builder builder = new AlertDialog.Builder(this);
         builder.setTitle("單選彈框標題");
         builder.setIcon(R.drawable.a12);
         builder.setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int which) {
                 String selectItem = items[which].toString();
                 Toast.makeText(MainActivity.this, selectItem, Toast.LENGTH_SHORT).show();
             }
         });
         builder.setPositiveButton("確認", new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int which) {
                 Toast.makeText(MainActivity.this, "確認", Toast.LENGTH_SHORT).show();
             }
         });
         builder.show();
     }
     /* 彈出 list 型彈框 */
     public void showListDialog() {
         final CharSequence[] items = new CharSequence[5];
         for (int i = 0; i < 5; i++) {
             items[i] = "數據" + i;
         }
         AlertDialog.Builder builder = new AlertDialog.Builder(this);
         builder.setTitle("List 型彈框標題");
         builder.setIcon(R.drawable.a12);
         builder.setItems(items, new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int which) {
                 String selectItem = items[which].toString();
                 Toast.makeText(MainActivity.this, selectItem, Toast.LENGTH_SHORT).show();
             }
         });
         builder.show();
     }
     /* 彈出簡單的彈框 */
     public void showSimpleDialog() {
         AlertDialog.Builder builder = new AlertDialog.Builder(this);
         builder.setTitle("普通彈框標題");
         builder.setIcon(R.drawable.a12);
         builder.setMessage("普通彈框提示信息...");
         builder.setPositiveButton("確認", new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int which) {
                 Toast.makeText(MainActivity.this, "確認", Toast.LENGTH_SHORT).show();
             }
         });
         builder.setNeutralButton("快點我", new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int which) {
                 Toast.makeText(MainActivity.this, "我就知道你要點我", Toast.LENGTH_SHORT).show();
             }
         });
         builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int which) {
                 Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
             }
         });
         builder.show();
     }
     /* 彈出自定義的彈框 */
     public void showCustomDialog() {
         final EditText userName, passWord;
         LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
         View view = inflater.inflate(R.layout.custom_dialog, null);
         userName = (EditText) view.findViewById(R.id.username_et);
         passWord = (EditText) view.findViewById(R.id.pas_et);
         AlertDialog.Builder builder = new AlertDialog.Builder(this);
         builder.setView(view);
         builder.setPositiveButton("登陸", new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int which) {
                 String nameStr = userName.getText().toString();
                 String passStr = passWord.getText().toString();
                 Toast.makeText(MainActivity.this, "用戶名:" + nameStr + ",密碼:" + passStr, Toast.LENGTH_SHORT).show();
             }
         });
         builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int which) {
                 Toast.makeText(MainActivity.this, "取消登錄", Toast.LENGTH_SHORT).show();
             }
         });
         builder.show();
     }
     /* 繼承實現監聽響應 */
     @Override
     public void onClick(View v) {
         switch (v.getId()) {
             case R.id.info_dg:
                 showSimpleDialog();
                 break;
             case R.id.list_dg:
                 showListDialog();
                 break;
             case R.id.single_dg:
                 showSingleDialog();
                 break;
             case R.id.multi_dg:
                 showMultiDialog();
                 break;
             case R.id.custom_dg:
                 showCustomDialog();
                 break;
         }
     }
 }

3.2 主佈局 activity_main.xml

 <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
     xmlns:tools="https://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:paddingBottom="@dimen/activity_vertical_margin"
     android:paddingLeft="@dimen/activity_horizontal_margin"
     android:paddingRight="@dimen/activity_horizontal_margin"
     android:paddingTop="@dimen/activity_vertical_margin"
     tools:context="com.example.dialog.MainActivity">
     <Button
         android:id="@+id/info_dg"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentLeft="true"
         android:layout_alignParentStart="true"
         android:layout_alignParentTop="true"
         android:text="提示型Dialog" />
     <Button
         android:id="@+id/list_dg"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentTop="true"
         android:layout_toEndOf="@+id/info_dg"
         android:layout_toRightOf="@+id/info_dg"
         android:text="List型Dialog" />
     <Button
         android:id="@+id/single_dg"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentTop="@+id/list_dg"
         android:layout_toEndOf="@+id/list_dg"
         android:layout_toRightOf="@+id/list_dg"
         android:text="單選型Dialog" />
     <Button
         android:id="@+id/multi_dg"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentLeft="true"
         android:layout_alignParentStart="true"
         android:layout_below="@+id/info_dg"
         android:text="多選型Dialog" />
     <Button
         android:id="@+id/custom_dg"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentTop="@+id/multi_dg"
         android:layout_toEndOf="@+id/multi_dg"
         android:layout_toRightOf="@+id/multi_dg"
         android:text="自定義Dialog" />
 </RelativeLayout>

3.3 自定義佈局 custom_dialog.xml

 <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:background="@color/colorAccent"
     android:padding="16dp">
     <TextView
         android:id="@+id/title_login"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentTop="true"
         android:layout_centerHorizontal="true"
         android:layout_gravity="center_horizontal"
         android:text="登陸"
         android:textAppearance="?android:attr/textAppearanceLarge" />
     <RelativeLayout
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentEnd="true"
         android:layout_alignParentRight="true"
         android:layout_below="@+id/title_login"
         android:layout_marginTop="10dp">
         <TextView
             android:id="@+id/username"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_alignBaseline="@+id/username_et"
             android:layout_alignParentLeft="true"
             android:layout_alignParentStart="true"
             android:layout_alignParentTop="true"
             android:text="用戶名:"
             android:textAppearance="?android:attr/textAppearanceLarge" />
         <EditText
             android:id="@+id/username_et"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_alignParentEnd="true"
             android:layout_alignParentRight="true"
             android:layout_alignParentTop="true"
             android:layout_toEndOf="@+id/username"
             android:layout_toRightOf="@+id/username"
             android:ems="10"
             android:hint="請輸入用戶名"
             android:inputType="textPersonName" />
         <EditText
             android:id="@+id/pas_et"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_alignLeft="@+id/username_et"
             android:layout_alignStart="@+id/username_et"
             android:layout_alignParentEnd="true"
             android:layout_alignParentRight="true"
             android:layout_below="@+id/username_et"
             android:ems="10"
             android:hint="請輸入密碼"
             android:inputType="textPassword" />
         <TextView
             android:id="@+id/password"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_alignBaseline="@+id/pas_et"
             android:layout_alignParentLeft="true"
             android:layout_alignParentStart="true"
             android:layout_alignTop="@+id/pas_et"
             android:text="密碼:"
             android:textAppearance="?android:attr/textAppearanceLarge" />
     </RelativeLayout>
 </RelativeLayout>

3.4 效果截圖

依次為:簡單 Dialog、List 型 Dialog、單選型 Dialog、多選型 Dialog、自定義 Dialog。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved