对话框AlertDialog的基本类型与创建

 

测试代码:

 布局:

activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.zzw.testalerdialog.MainActivity" >

    <Button
        android:id="@+id/horizontally"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="横向显示" />

    <Button
        android:id="@+id/vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:paddingTop="20dp"
        android:text="竖向显示" />

    <Button
        android:id="@+id/singleChoice"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:paddingTop="20dp"
        android:text="单选框显示" />

    <Button
        android:id="@+id/multiChoice"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:paddingTop="20dp"
        android:text="多选框显示" />

    <Button
        android:id="@+id/myDialog"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:paddingTop="20dp"
        android:text="自定义显示" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_weight="1"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

 JAVA代码:

package com.zzw.testalerdialog;

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

public class MainActivity extends Activity implements View.OnClickListener {

    private Context context;

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

        init();
    }

    private void init() {

        context = this;

        findViewById(R.id.horizontally).setOnClickListener(this);
        findViewById(R.id.vertical).setOnClickListener(this);
        findViewById(R.id.singleChoice).setOnClickListener(this);
        findViewById(R.id.multiChoice).setOnClickListener(this);
        findViewById(R.id.myDialog).setOnClickListener(this);
        
    }

    // 横向类型显示
    private void horizontallyShow() {

        AlertDialog dialog = new AlertDialog.Builder(context).create();
        dialog.setIcon(R.drawable.ic_launcher);
        dialog.setTitle("横向显示");
        dialog.setMessage("提示信息");
        // DialogInterface.BUTTON_POSITIVE作用是显示的顺序
        dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(context, "确定", 0).show();
            }
        });

        dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(context, "取消", 0).show();
            }
        });

        dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "考虑", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(context, "考虑", 0).show();
            }
        });

        dialog.show();
    }

    // 竖向类型显示
    private void verticalShow() {

        final String[] items = new String[3];
        for (int i = 0; i < 3; i++) {
            items[i] = "选项--" + i;
        }

        AlertDialog dialog = new AlertDialog.Builder(context).setItems(items, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                Toast.makeText(context, items[which], Toast.LENGTH_SHORT).show();
            }
        }).create();

        dialog.setTitle("竖向显示");
        dialog.setIcon(R.drawable.ic_launcher);

        dialog.show();

    }

    // 单选框类型显示
    private void singleChoiceShow() {

        final String[] items = new String[3];
        for (int i = 0; i < 3; i++) {
            items[i] = "选项--" + i;
        }

        AlertDialog.Builder mBuilder = new AlertDialog.Builder(context);
        // mBuilder.setIcon(R.drawable.ic_launcher);
        // mBuilder.setTitle("单选框显示");

        // checkedItem默认选择的位置参数
        mBuilder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(context, items[which], Toast.LENGTH_SHORT).show();
                dialog.dismiss();

            }
        });

        AlertDialog dialog = mBuilder.create();
        dialog.setIcon(R.drawable.ic_launcher);
        dialog.setTitle("单选框显示");
        dialog.show();

    }

    // 复选框类型显示
    private void multiChoiceShow() {

        final String[] items = new String[3];
        for (int i = 0; i < 3; i++) {
            items[i] = "选项--" + i;
        }

        AlertDialog.Builder mBuilder = new AlertDialog.Builder(context);

        // checkedItems为默认勾选的状态
        final boolean[] checkedItems = { false, true, false };
        // 这个监听的作用是用于检测item选中状态的变化
        mBuilder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {

            }
        });

        AlertDialog dialog = mBuilder.create();
        dialog.setIcon(R.drawable.ic_launcher);
        dialog.setTitle("复选框显示");

        // 用于确定,知道用户勾选了那些选项
        dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                String s = "";
                for (int i = 0; i < items.length; i++) {
                    if (checkedItems[i])
                        s += items[i] + "\n";
                }
                Toast.makeText(context, s, Toast.LENGTH_SHORT).show();
            }
        });

        // 用于取消
        dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "取消", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });

        dialog.show();
    }

    
    //自定义类型显示
    private void myDialogShow(){
        
        AlertDialog.Builder mBuilder = new AlertDialog.Builder(context);
        
        mBuilder.setView(getLayoutInflater().inflate(R.layout.my_dialog, null));
        
        AlertDialog dialog=mBuilder.create();
//        dialog.setIcon(R.drawable.ic_launcher);
//        dialog.setTitle("自定义的对话框");
        dialog.show();
    }
    
    @Override
    public void onClick(View v) {

        switch (v.getId()) {
        case R.id.horizontally:
            
            horizontallyShow();
            break;
        case R.id.vertical:
            
            verticalShow();
            break;
        case R.id.singleChoice:

            singleChoiceShow();
            break;
        case R.id.multiChoice:

            multiChoiceShow();
            break;
        case R.id.myDialog:
            myDialogShow();
            break;
            
        }
    }

}

自定义的布局my_dialog.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#80CBC4"
    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_horizontal"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="www.cnblogs.com/zzw1994"
        android:textColor="@android:color/holo_red_light" />

</LinearLayout>




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值