AlertDialog(提示对话框)

简单的总结为五种方式:
1、带消息和按钮的提示对话框;
2、带列表和按钮的列表对话框;
3、带单选列表项和按钮的单选列表对话框;
4、带复选列表项和按钮的复选列表对话框;
5、自定义对话框。

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.menumy.MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:onClick="onClick"
        android:text="带消息和按钮" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:onClick="onClick"
        android:text="带列表和按钮" />

    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button2"
        android:onClick="onClick"
        android:text="带单选列表和按钮" />

    <Button
        android:id="@+id/button4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button3"
        android:onClick="onClick"
        android:text="带复选列表和按钮" />

    <Button
        android:id="@+id/button5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button4"
        android:onClick="onClick"
        android:text="自定义对话框" />

</RelativeLayout>

自定义对话框布局:

<?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="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="用户名"
            android:textSize="15sp" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="密码"
            android:textSize="15sp" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>

</LinearLayout>
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {

    private String cours[] = new String[] { "一", "二", "三", "四", "五" };
    private boolean checkedItems[] = new boolean[] { true, false, true, true, false };
    // AlertDialog类的构造方法全部都是protected的,通过其内部类AlertDialog.Builder来创建
    private AlertDialog.Builder alertBuilder;
    private Dialog dialog;

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

    }

    public void onClick(View v) {
        switch (v.getId()) {
        // 带消息和按钮的提示对话框
        case R.id.button1:
            alertBuilder = new AlertDialog.Builder(this);
            alertBuilder.setTitle("请认真选择!");// 标题
            alertBuilder.setMessage("你还好吗?");// 提示信息
            alertBuilder.setIcon(R.drawable.ic_launcher);// 图标
            alertBuilder.setPositiveButton("好", new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO 比如跳转Activity
                }
            });

            alertBuilder.setNegativeButton("不好", new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO
                }
            });

            alertBuilder.setNeutralButton("还好", new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO
                }
            });

            // 创建对话框
            dialog = alertBuilder.create();
            // 显示对话框
            dialog.show();
            break;

        // 带列表和按钮的列表对话框
        case R.id.button2:
            alertBuilder = new AlertDialog.Builder(this);
            alertBuilder.setTitle("请认真选择!");
            alertBuilder.setIcon(R.drawable.ic_launcher);
            alertBuilder.setItems(cours, new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(MainActivity.this, cours[which], Toast.LENGTH_SHORT).show();
                }
            });
            alertBuilder.setPositiveButton("好", new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO
                }
            });

            alertBuilder.setNegativeButton("还好", new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO
                }
            });
            dialog = alertBuilder.create();
            dialog.show();
            break;

        // 带单选列表项和按钮的单选列表对话框
        case R.id.button3:
            alertBuilder = new AlertDialog.Builder(this);
            alertBuilder.setTitle("请认真选择!");
            alertBuilder.setIcon(R.drawable.ic_launcher);
            alertBuilder.setSingleChoiceItems(cours, 1, new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(MainActivity.this, cours[which], Toast.LENGTH_SHORT).show();
                }
            });
            alertBuilder.setPositiveButton("好", new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO
                }
            });

            alertBuilder.setNegativeButton("还好", new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO
                }
            });
            dialog = alertBuilder.create();
            dialog.show();
            break;

        // 带复选列表项和按钮的复选列表对话框
        case R.id.button4:
            alertBuilder = new AlertDialog.Builder(this);
            alertBuilder.setTitle("请认真选择!");
            alertBuilder.setIcon(R.drawable.ic_launcher);
            alertBuilder.setMultiChoiceItems(cours, checkedItems, new OnMultiChoiceClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                    Toast.makeText(MainActivity.this, cours[which], Toast.LENGTH_SHORT).show();
                }
            });
            alertBuilder.setPositiveButton("好", new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO
                }
            });

            alertBuilder.setNegativeButton("还好", new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO
                }
            });
            dialog = alertBuilder.create();
            dialog.show();
            break;

        // 自定义对话框
        case R.id.button5:
            LayoutInflater layoutInflater = LayoutInflater.from(this);
            View loginView = layoutInflater.inflate(R.layout.login, null);

            alertBuilder = new AlertDialog.Builder(this);
            alertBuilder.setTitle("请认真选择!");
            alertBuilder.setIcon(R.drawable.ic_launcher);
            alertBuilder.setView(loginView);
            alertBuilder.setPositiveButton("好", new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO
                }
            });

            alertBuilder.setNegativeButton("还好", new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO
                }
            });
            dialog = alertBuilder.create();
            dialog.show();
        default:
            break;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值
>