(亲测) 带有单选按钮和复选按钮 的dialog+详细代码+注释

带有单选按钮的dialog:

package example.com.myapplication;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends Activity {
    //声明选中项变量
    private int selectedCityIndex = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //定义城市数组
        final String[] arrayCity = new String[] { "杭州", "纽约", "威尼斯", "北海道" };

        //实例化AlertDialog对话框
        Dialog alertDialog = new AlertDialog.Builder(this)
                .setTitle("你最喜欢哪个地方?")                        //设置标题
                .setIcon(R.mipmap.ic_launcher)                //设置图标
                //设置对话框显示一个单选List,指定默认选中项,同时设置监听事件处理
                .setSingleChoiceItems(arrayCity, 0, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        selectedCityIndex = which;               //选中项的索引保存到选中项变量
                    }
                })
                //添加取消按钮并增加监听处理
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                    }
                })
                //添加确定按钮并增加监听处理
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getApplication(), arrayCity[selectedCityIndex], Toast.LENGTH_SHORT).show();
                    }
                })
                .create();
        alertDialog.show();
    }
}
 
 
带有复选按钮的dialog代码:;
 
 
package example.com.myapplication;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;



public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //定义运动数组
        final String[] arraySport = new String[] { "足球", "篮球", "网球", "乒乓球" };
        final boolean[] arraySportSelected = new boolean[] {false, false, false, false};

        //实例化AlertDialog对话框
        Dialog alertDialog = new AlertDialog.Builder(this)
                .setTitle("你喜欢哪些运动?")                        //设置标题
                .setIcon(R.mipmap.ic_launcher)               //设置图标
                //设置对话框显示一个复选List,指定默认选中项,同时设置监听事件处理
                .setMultiChoiceItems(arraySport, arraySportSelected, 
                        new DialogInterface.OnMultiChoiceClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        arraySportSelected[which] = isChecked;              //选中项的布尔真假保存到选中项变量
                    }
                })
                //添加取消按钮并增加监听处理
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        StringBuilder stringBuilder = new StringBuilder();
                        for (int i = 0; i < arraySportSelected.length; i++) {
                            if (arraySportSelected[i] == true){
                                stringBuilder.append(arraySport[i] + "、");
                            }
                        }
                        Toast.makeText(getApplication(), stringBuilder.toString(), Toast.LENGTH_SHORT).show();
                    }
                })

                //添加确定按钮并增加监听处理
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                    }
                })
                .create();

        alertDialog.show();
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值