简单单选、多选按钮设计及监听


布局
<?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.edu.jereh.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"
        android:layout_alignParentStart="true" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/bt3"
        android:text="多选对话框"
        android:layout_below="@+id/bt2"
        android:layout_alignParentStart="true" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/bt4"
        android:text="自定义对话框"
        android:layout_below="@+id/bt3"
        android:layout_alignParentStart="true" />


</RelativeLayout>
监听
package com.edu.jereh.jreduch01;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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() {
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(getBaseContext(), "点击了确定", Toast.LENGTH_SHORT).show();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(getBaseContext(), "点击了取消", Toast.LENGTH_SHORT).show();
                    }//.show()是显示" "中的字
                });
                builder.setCancelable(false);//点击其他空白位置不取消
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
            }
        });
        bt2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(AlertActivity.this);
                builder.setTitle("请选择:");
                builder.setIcon(R.mipmap.ic_launcher);
                final String[] items = {"北京", "上海", "杭州", "烟台"};
                builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getBaseContext(), items[which].toString(), Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                    }
                });
                builder.setCancelable(false);
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
            }
        });
        bt3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(AlertActivity.this);
                builder.setTitle("请选择");
                builder.setIcon(R.mipmap.ic_launcher);
                final String[] items = {"北京", "上海", "青岛", "广州", "烟台"};
               // boolean[] choice = {true, false, true, false, true};
                final boolean[] itemChoice = new boolean[items.length];
                itemChoice[0] = true;
                itemChoice[1] = true;
                builder.setMultiChoiceItems(items, itemChoice, new DialogInterface.OnMultiChoiceClickListener() {
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        if (isChecked) {
                            Toast.makeText(getBaseContext(), items[which].toString(), Toast.LENGTH_SHORT).show();
                            itemChoice[which] = true;
                        } else {
                            itemChoice[which] = false;
                        }
                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        String str = "";
                        for (int i = 0; i < items.length; i++) {
                            str += (itemChoice[i] == true) ? items[i] : " ";
                        }
                        Toast.makeText(getBaseContext(), str, Toast.LENGTH_SHORT).show();
                    }
                });
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
            }
        });
        bt4.setOnClickListener(new View.OnClickListener(){
            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(){
                    public  void onClick(View view){
                        Toast.makeText(AlertActivity.this,"你点击了图片",Toast.LENGTH_SHORT).show();
                    }
                });
                AlertDialog.Builder builder = new AlertDialog.Builder(AlertActivity.this);
                builder.setView(v);
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
            }
        });
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值