Android 对对话框进行监听

首先进行布局,创建按钮

 

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="com.example.administrator.jreduch01.AlertActivity">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/bt1"
        android:text="对话框"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/bt2"
        android:text="对话框单选"
        android:layout_below="@id/bt1"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/bt3"
        android:text="对话框多选"
        android:layout_below="@id/bt2"/>
   <Button
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/bt4"
       android:text="自定义对话框"
       android:layout_below="@id/bt3"/>
</RelativeLayout>
 
 
对控件进行监听
 
package com.example.administrator.jreduch01;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;


public class AlertActivity extends AppCompatActivity {
    private Button bt1;
    private Button bt2;
    private Button bt3;
    private Button bt4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alert);
        bt1=(Button)findViewById(R.id.bt1);
        bt2=(Button)findViewById(R.id.bt2);
        bt3=(Button)findViewById(R.id.bt3);
        bt4=(Button)findViewById(R.id.bt4);
        //设置对话框
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder=
                        new AlertDialog.Builder(AlertActivity.this);
                builder.setTitle("提示");
                builder.setIcon(R.mipmap.ic_launcher);//建立图标
                builder.setMessage("确定......吗?");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getBaseContext(), "点击了确定", Toast.LENGTH_SHORT).show();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getBaseContext(), "点击了取消", Toast.LENGTH_SHORT).show();
                    }
                });
                builder.setCancelable(false);//点击除了取消,确定外的按钮不能取消
                AlertDialog alertDialog=builder.create();//生成提示框
                alertDialog.show();//显示出来
            }
        });
        //设置单选框
        bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder=new AlertDialog.Builder(AlertActivity.this);
                builder.setTitle("请选择");
                builder.setIcon(R.mipmap.ic_launcher);
                builder.setCancelable(false);//点击选项以外的地方对话框消失
                final String[] items={"北京","上海","烟台"};
                builder.setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int i) {
                        Toast.makeText(getBaseContext(), items[i].toString(), Toast.LENGTH_SHORT).show();
                        dialog.dismiss();//点击后对话框消失
                    }
                });
                AlertDialog alertDialog=builder.create();
                alertDialog.show();
            }
        });
        //设置多选框
        bt3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder=new AlertDialog.Builder(AlertActivity.this);
                builder.setTitle("请选择");
                builder.setIcon(R.mipmap.ic_launcher);
                final String[] items={"北京","上海","烟台","广州"};
                final boolean[] choice={true,true,false,false};//final保持对它的引用
                final boolean[] itemchoice=new boolean[items.length];
                itemchoice[0]=true;
                itemchoice[1]=true;
                builder.setMultiChoiceItems(items, choice, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int i, boolean b) {
                        if (b) {
                            Toast.makeText(getBaseContext(), items[i].toString(), Toast.LENGTH_SHORT).show();
                            itemchoice[i] = true;
                        } else {
                            itemchoice[i] = false;
                        }
                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //建立一个循环实现选择内容的累加
                        String str = "";
                        for (int ch = 0; ch < items.length; ch++) {
                            str += (itemchoice[ch] == true) ? items[ch] : "";
                        }
                        Toast.makeText(getBaseContext(), str, Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                    }
                });
                AlertDialog alertDialog=builder.create();
                alertDialog.show();
            }
        });
        //自定义布局
        bt4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                View v=getLayoutInflater().inflate(R.layout.toast_layout,null);
                ImageView iv=(ImageView)v.findViewById(R.id.toast_iv);
                iv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(getBaseContext(),"点击了图片",Toast.LENGTH_SHORT).show();
                    }
                });
                AlertDialog.Builder builder=new AlertDialog.Builder(AlertActivity.this);
                builder.setView(v);
                AlertDialog alertDialog=builder.create();
                alertDialog.show();
            }
        });

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值