Android攻城狮的第二门课(第2季)第4章 使用AlertDialog实现提示框

通过本章学习AlertDialog提示框,让大家熟悉我们应用开发过程中常用的提示框弹出方式,弹出式对话框的各种设置和创建。

Dialog对话框 在当前界面弹出的小窗口,用于显示重要提示信息,提示用户输入信息,确认信息,或者显示某种状态。
一般情况下,用户要与对话框进行交互,然后返回到被覆盖的界面以继续运行当前的应用程序。

常见的Dialog
(1)确认对话框
(2)单选按钮对话框
(3)多选按钮对话框
(4)列表对话框

<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=".MainActivity" >
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:id="@+id/dialog_btn1"
        android:text="确认对话框"
        />
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:id="@+id/dialog_btn2"
        android:text="单选按钮对话框"
        />
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:id="@+id/dialog_btn3"
        android:text="多选按钮对话框"
        />
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:id="@+id/dialog_btn4"
        android:text="列表对话框"
        />
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:id="@+id/dialog_btn5"
        android:text="自定义对话框"
        />
</LinearLayout>
package com.imooc.dialog.dialogdemo;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class MainActivity extends Activity {
    String [] single_list = {"男","女","女博士","程序员"};
    String [] multi_list = {"篮球","足球","男生","女生"};
    String [] item_list = {"项目经理","策划","测试","美工","程序猿"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initEvent();
    }

    /**
     * 初始化点击事件
     */
    private void initEvent() {
        findViewById(R.id.dialog_btn1).setOnClickListener(
                new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        showDialog1();
                    }
                });
        findViewById(R.id.dialog_btn2).setOnClickListener(
                new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        showDialog2();
                    }
                });
        findViewById(R.id.dialog_btn3).setOnClickListener(
                new OnClickListener() { //设置点击事件
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        showDialog3();
                    }
                });
        findViewById(R.id.dialog_btn4).setOnClickListener(
                new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        showDialog4();
                    }
                });
        findViewById(R.id.dialog_btn5).setOnClickListener(
                new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        showDialog5();
                    }
                });
    }

    /**
     * 显示确认对话框
     */
    private void showDialog1() {
        //创建Dialog并显示
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("确认对话框");//设置标题
        builder.setIcon(R.drawable.ic_launcher);//设置图标
        builder.setMessage("确认对话框提示内容");//设置内容
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                Toast.makeText(MainActivity.this, "点击了确定按钮!",
                        Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消",  new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                Toast.makeText(MainActivity.this, "点击了取消按钮!",
                        Toast.LENGTH_SHORT).show();
            }
        });
        AlertDialog dialog = builder.create();//获取dialog
        dialog.show();//显示对话框
    }
    /**
     * 显示单选按钮对话框
     */
    private void showDialog2() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("选择性别");//设置标题
        builder.setIcon(R.drawable.ic_launcher);//设置图标
        //single_list 单选对话框的描述   0 默认第一个选择
        builder.setSingleChoiceItems(single_list, 0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                String str = single_list[which]; //从single_list 选择单选对话框的描述
                Toast.makeText(MainActivity.this, "这个人是"+str+"!",
                        Toast.LENGTH_SHORT).show();
            }
        });
        AlertDialog dialog = builder.create();//获取dialog
        dialog.show();//显示对话框
    }
    /**
     * 显示多选按钮对话框
     */
    private void showDialog3() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("爱好");//设置标题
        builder.setIcon(R.drawable.ic_launcher);//设置图标
        //multi_list 多选描述     null  默认选中的按钮
        builder.setMultiChoiceItems(multi_list, null, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                // TODO Auto-generated method stub
                if(isChecked){
                    Toast.makeText(MainActivity.this, "我喜欢上了"+multi_list[which]+"!",
                            Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(MainActivity.this, "我不喜欢"+multi_list[which]+"了!",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                dialog.dismiss(); //按钮:隐藏对话
            }
        });
        AlertDialog dialog = builder.create();//获取dialog
        dialog.show();//显示对话框
    }

    /**
     * 显示列表对话框
     */
    private void showDialog4() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("部门列表");//设置标题
        builder.setIcon(R.drawable.ic_launcher);//设置图标
        builder.setItems(item_list, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                Toast.makeText(MainActivity.this, "我动了"+item_list[which]+"!",
                        Toast.LENGTH_SHORT).show();
            }
        });
        AlertDialog dialog = builder.create();//获取dialog
        dialog.show();//显示对话框
    }
    /**
     * 显示自定义对话框
     */
    private void showDialog5() {
        //获取自定义布局  并设置为  view属性对象
        LayoutInflater inflater = LayoutInflater.from(this);//加载布局
        View view = inflater.inflate(R.layout.dialog_layout, null);

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("自定义对话框");//设置标题
        builder.setIcon(R.drawable.ic_launcher);//设置图标
        builder.setView(view); //设置自定义对话框
        AlertDialog dialog = builder.create();//获取dialog
        dialog.show();//显示对话框
    }
}

自定义对话框

自定义布局 dialog_layout.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" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="输入内容..."
            android:layout_weight="1"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="提交"
            android:layout_marginLeft="10dip"
            />
    </LinearLayout>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dip"
        android:src="@drawable/topimg"
        />
</LinearLayout>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值