AlertDialog对话框简单案例

什么是Dialog?

  Dialog类,是一切对话框的基类,需要注意的是,Dialog类虽然可以在界面上显示,但是并非继承于View类,而是直接从java.lang.Object开始构造出的。类似于Activity,Dialog也是有生命周期的,它的生命周期由Activity来维护。Activity负责生成、保存、恢复它。在生命周期的每一个阶段都有一些回调函数供系统反向调用。

什么是AlertDialog?什么是AlertDialog.Builder?
        AlertDialog是Dialog的一个直接子类,一个AlertDialog可以有两个Button或者3个Button,可以对一个AlertDialog设置title、message。不能直接通过AlertDialog的构造函数来生成一个AlertDialog,一般生成的时候都是通过它的的一个内部静态类AlertDialog.Builder来构造的。

下列是实例图:

MainActivity.java

package com.shaoxin.myapplication;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;
    private Button btn5;


    private MyBtnClick btnClick;
    private Dialog dialog;
    private String[] strCitys = {"南昌", "上海", "北京"};
    private int cityIndex = 0;
    private List data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        setListener();
    }

    private void setListener() {
        btnClick = new MyBtnClick();
        data = new ArrayList();
        btn1.setOnClickListener(btnClick);
        btn2.setOnClickListener(btnClick);
        btn3.setOnClickListener(btnClick);
        btn4.setOnClickListener(btnClick);
        btn5.setOnClickListener(btnClick);
    }

    private void init() {
        btn1 = (Button) findViewById(R.id.btn_1);
        btn2 = (Button) findViewById(R.id.btn_2);
        btn3 = (Button) findViewById(R.id.btn_3);
        btn4 = (Button) findViewById(R.id.btn_4);
        btn5 = (Button) findViewById(R.id.btn_5);
    }

    private class MyBtnClick implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.btn_5: {
                    final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MainActivity.this);
                    dialogBuilder.setTitle("单选对话框");
                    dialogBuilder.setIcon(R.mipmap.ic_launcher);
                    View view_1 = View.inflate(MainActivity.this, R.layout.dialog_item, null);
                    dialogBuilder.setView(view_1);
                    dialogBuilder.setNeutralButton("确定", null);
                    dialogBuilder.create().show();
                }
                break;
                case R.id.btn_4: {
                    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MainActivity.this);
                    dialogBuilder.setTitle("多选对话框");
                    dialogBuilder.setIcon(R.mipmap.ic_launcher);
                    data.clear();
                    dialogBuilder.setMultiChoiceItems(R.array.citys, new boolean[]{false, false, false},
                            new DialogInterface.OnMultiChoiceClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                                    String[] citys = getResources().getStringArray(R.array.citys);

                                    if (isChecked) {
                                        data.add(citys[which]);
                                    } else {
                                        data.remove(citys[which]);
                                    }
                                }
                            });
                    dialogBuilder.setNeutralButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Log.v("msg", data.toString());
                        }
                    });
                    dialogBuilder.create().show();
                }
                break;
                case R.id.btn_3: {
                    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MainActivity.this);
                    dialogBuilder.setTitle("单选对话框");
                    dialogBuilder.setIcon(R.mipmap.ic_launcher);
                    dialogBuilder.setSingleChoiceItems(R.array.citys, 0, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //获取内容
                            cityIndex = which;

                        }
                    });
                    dialogBuilder.setNeutralButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            String[] citys = getResources().getStringArray(R.array.citys);
                            Log.v("msg", citys[cityIndex]);
                        }
                    });
                    dialogBuilder.create().show();
                }
                break;
                case R.id.btn_2: {
                    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MainActivity.this);
                    dialogBuilder.setTitle("简单ITEM对话框");
                    dialogBuilder.setIcon(R.mipmap.ic_launcher);
                    dialogBuilder.setItems(strCitys, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Log.v("msg", strCitys[which]);
                        }
                    });
                    dialogBuilder.create().show();
                }
                break;
                case R.id.btn_1: {
                    Log.v("msg", "sdf");
                    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MainActivity.this);
                    dialogBuilder.setTitle("简单对话框");
                    dialogBuilder.setIcon(R.mipmap.ic_launcher);
                    dialogBuilder.setMessage("是否退出对话框?");
                    dialogBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            finish();
                        }
                    });
                    dialogBuilder.setNegativeButton("取消", null);
                    dialogBuilder.setNeutralButton("更多", null);
                    dialogBuilder.create().show();
                }

                break;
            }
        }
    }
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.shaoxin.myapplication.MainActivity">

    <Button
        android:id="@+id/btn_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="简单对话框" />

    <Button
        android:id="@+id/btn_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="简单item对话框" />

    <Button
        android:id="@+id/btn_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="单选对话框" />

    <Button
        android:id="@+id/btn_4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="多选对话框" />

    <Button
        android:id="@+id/btn_5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="自定义对话框" />
</LinearLayout>
dialog_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@mipmap/ic_launcher" />
</LinearLayout>

arrays.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="citys">
        <item>南昌</item>
        <item>北京</item>
        <item>上海</item>
    </string-array>
</resources>

将代码导入直接运行即可


posted @ 2016-12-04 20:13  情似雨餘黏地絮  阅读(242)  评论(0编辑  收藏  举报