Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> android中提示&對話框----AlertDialog,android提示

android中提示&對話框----AlertDialog,android提示

編輯:關於android開發

android中提示&對話框----AlertDialog,android提示


AlertDialog(對話框)

一、對話框的基本使用流程

step1:創建AlertDialog.Buider;

step2:調用setIcon()設置圖標,setTitle()或者setCustomerTitle設置標題

step3:設置對話框的內容setMessage()還有其他方式;

step4:setPosition/Negative/NaturalButton設置:確定、取消、中立

step5:調用create()方法創建這個對象,在調用show()方法將對話框顯示出來

二、幾種常用的對話框已經自定義view的對話框使用實例

package com.example.test3;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements View.OnClickListener{
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;
    private Button btn5;
    private AlertDialog.Builder builder;
    private AlertDialog alertDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);
        btn4 = (Button) findViewById(R.id.btn4);
        btn5 = (Button) findViewById(R.id.btn5);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()){
            case R.id.btn1:{
                builder = new AlertDialog.Builder(MainActivity.this);
                alertDialog = builder.setIcon(R.mipmap.ic_launcher)
                        .setTitle("系統提示:")
                        .setMessage("這是一個最普通AlertDialog,\n帶有三個按鈕,分別是取消、確定、中立")
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        })
                        .setPositiveButton("確定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        })
                        .setNeutralButton("中立", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        }).create();
                alertDialog.show();
                break;
            }
            case R.id.btn2:{
                final String[] lesson = new String[]{"語文", "數學", "英語", "化學", "生物", "物理", "體育"};
                builder = new AlertDialog.Builder(MainActivity.this);
                alertDialog= builder.setIcon(R.mipmap.ic_launcher)
                        .setTitle("選擇你喜歡的課程")
                        .setItems(lesson, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(getApplicationContext(), "你選擇了" + lesson[which], Toast.LENGTH_SHORT).show();
                            }
                        }).create();
                alertDialog.show();
                break;
            }
            case R.id.btn3:{
                final String[] fruits = new String[]{"蘋果", "雪梨", "香蕉", "葡萄", "西瓜"};
                builder = new AlertDialog.Builder(MainActivity.this);
                alertDialog = builder.setIcon(R.mipmap.ic_launcher)
                        .setTitle("選擇你喜歡的水果,只能選一個哦~")
                        .setSingleChoiceItems(fruits, 0, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(getApplicationContext(), "你選擇了" + fruits[which], Toast.LENGTH_SHORT).show();
                            }
                        }).create();
                alertDialog.show();
                break;
            }
            case R.id.btn4:{
                final String[] menu = new String[]{"水煮豆腐", "蘿卜牛腩", "醬油雞", "胡椒豬肚雞"};
                //定義一個用來記錄個列表項狀態的boolean數組
                final boolean[] checkItems = new boolean[]{false, false, false, false};
                builder = new AlertDialog.Builder(MainActivity.this);
                alertDialog = builder.setIcon(R.mipmap.ic_launcher)
                        .setMultiChoiceItems(menu, checkItems, new DialogInterface.OnMultiChoiceClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                                checkItems[which] = isChecked;
                            }
                        })
                        .setPositiveButton("確定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String result = "";
                                for (int i = 0; i < checkItems.length; i++) {
                                    if (checkItems[i])
                                        result += menu[i] + " ";
                                }
                                Toast.makeText(getApplicationContext(), "客官你點了:" + result, Toast.LENGTH_SHORT).show();
                            }
                        })
                        .create();
                alertDialog.show();
                break;
            }
            case R.id.btn5:{
                builder = new AlertDialog.Builder(MainActivity.this);
                View view1 = getLayoutInflater().inflate(R.layout.view_dialog,null);
                builder.setView(view1);
                alertDialog = builder.create();
                alertDialog.show();
                Button btn1 = (Button) view1.findViewById(R.id.btn_cancle);
                Button btn2 = (Button) view1.findViewById(R.id.btn_blog);
                Button btn3 = (Button) view1.findViewById(R.id.btn_close);
                btn1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        alertDialog.dismiss();
                    }
                });
                btn2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        alertDialog.dismiss();
                    }
                });
                btn3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        alertDialog.dismiss();
                    }
                });
                break;
            }
        }
    }
}

效果圖

 

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