Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android Composite(Dialog)詳解

Android Composite(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類:
AlertDialog即我們使用的窗口類,可以通過AlertDialog類的相關方法設置Dialog的屬性以及顯示,通常我們通過他的內部類Builder來構建他的實例,主要原因在於它的構造方法都是受保護的。

2.2 AlertDialog.Builder類:
AlertDialog.Builder即AlertDialog內的類,通過它來實例化Dialog,同時也可以通過他來設置AlertDialog的屬性資源和
Dialog的顯示

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;

import java.util.ArrayList;

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
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //設置AlertDialog標題
        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
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //設置AlertDialog標題
        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
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //設置AlertDialog標題
        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
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //設置AlertDialog標題
        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 =getLayoutInflater();
        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
        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



3.3 自定義布局custom_dialog.xml



    

    

        

        

        

        
    




3.4 效果截圖(依次為:簡單dialog,List型Dialog實現,單選型Dialog,多選型Dialog,自定義Dialog)

\ \\

\ \
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved