• 【Andriod-AlertDialog控件】 弹出对话框AlertDialog用法


    Result:

    Code:

    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.DialogInterface;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;
    
    public class MainActivity extends Activity implements OnClickListener
    {
        Button buttonSure, buttonRadio, buttonCheck, buttonList, buttonCustom;
    
        private String[] sexList =
        { "男", "女" };// 单选列表
        private String[] likeList =
        { "篮球", "足球", "打游戏", "听音乐", "看电影" };// 多选列表
        private String[] itemList =
        { "项目经理", "策划", "测试", "美工", "程序员" };// 列表
    
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            buttonSure = (Button) findViewById(R.id.buttonSure);
            buttonRadio = (Button) findViewById(R.id.buttonRadio);
            buttonCheck = (Button) findViewById(R.id.buttonCheck);
            buttonList = (Button) findViewById(R.id.buttonList);
            buttonCustom = (Button) findViewById(R.id.buttonCustom);
    
            buttonSure.setOnClickListener(this);
            buttonRadio.setOnClickListener(this);
            buttonCheck.setOnClickListener(this);
            buttonList.setOnClickListener(this);
            buttonCustom.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View v)
        {
            switch (v.getId())
            {
            case R.id.button:
    
                break;
            case R.id.buttonSure:
                sureDialog();
                break;
            case R.id.buttonRadio:
                radioDialog();
                break;
            case R.id.buttonCheck:
                checkDailog();
                break;
            case R.id.buttonList:
                listDialog();
                break;
            case R.id.buttonCustom:
                customDialog();
                break;
            default:
                break;
            }
        }
    
        /**
         * 确认对话框
         */
        private void sureDialog()
        {
            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 arg0, int arg1)
                {
                    Toast.makeText(MainActivity.this, "点击了确定按钮", Toast.LENGTH_SHORT).show();
                }
            });
            /* 添加对话框中取消按钮和点击事件 */
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface arg0, int arg1)
                {
                    Toast.makeText(MainActivity.this, "你点击了取消按钮", Toast.LENGTH_SHORT).show();
                }
            });
            AlertDialog dialog = builder.create();// 获取dialog
            dialog.show();// 显示对话框
        }
    
        /**
         * 单选确认对话框
         */
        private void radioDialog()
        {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("性别");// 设置标题
            builder.setIcon(R.drawable.ic_launcher);// 设置图标
            /* 参数一位单选列表文字,参数二为默认第几个选中(-1默认不选中),参数三是创建监听器 */
            builder.setSingleChoiceItems(sexList, -1, new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int which)
                {
                    String sex = sexList[which];
                    Toast.makeText(MainActivity.this, "这个人性别为" + sex, Toast.LENGTH_SHORT).show();
                }
            });
    
            /* 添加对话框中取消按钮和点击事件 */
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int which)
                {
                    dialog.dismiss();// 关闭对话框
                }
            });
            AlertDialog dialog = builder.create();// 获取dialog
            dialog.show();// 显示对话框
        }
    
        /**
         * 多选对话框
         */
        private void checkDailog()
        {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("爱好");// 设置标题
            builder.setIcon(R.drawable.ic_launcher);// 设置图标
            /* 参数同单选对话框一样,另外第二个参数默认不选中为null,而不是-1 */
            builder.setMultiChoiceItems(likeList, null, new DialogInterface.OnMultiChoiceClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int which, boolean isChecked)
                {
                    if (isChecked)
                    {
                        Toast.makeText(MainActivity.this, "我喜欢" + likeList[which], Toast.LENGTH_SHORT).show();
                    } else
                    {
                        Toast.makeText(MainActivity.this, "我不喜欢" + likeList[which], Toast.LENGTH_SHORT).show();
                    }
                }
            });
            /* 添加对话框中取消按钮点击事件 */
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int which)
                {
                    dialog.dismiss();// 关闭对话框
                }
            });
            AlertDialog dialog = builder.create();// 获取dialog
            dialog.show();// 显示对话框
        }
    
        /**
         * 列表对话框
         */
        private void listDialog()
        {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("部门列表");// 设置标题
            builder.setIcon(R.drawable.ic_launcher);// 设置图标
            builder.setItems(itemList, new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int which)
                {
                    Toast.makeText(MainActivity.this, "我点击了" + itemList[which], Toast.LENGTH_SHORT).show();
                }
            });
            AlertDialog dialog = builder.create();// 获取dialog
            dialog.show();// 显示对话框
        }
    
        /**
         * 自定义对话框
         */
        private void customDialog()
        {
            LayoutInflater inflater = LayoutInflater.from(this);
            View view = inflater.inflate(R.layout.dialog, 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();// 显示对话框
        }
    
    }
  • 相关阅读:
    SSIS 小脚本 文件路径验证
    SSIS 小脚本 时间参数验证
    索引 唯一索引设计指南
    容易遗忘的一些小代码之 ROW_NUMBER 和去重
    MSDNMDX#001 多维表达式 (MDX) 参考
    数据库引擎 页和区体系结构页和区
    数据库引擎 文件和文件组体系结构
    索引 索引键的最大大小
    索引 在文件组上放置索引
    索引 常规索引设计指南
  • 原文地址:https://www.cnblogs.com/Sukie-s-home/p/7205276.html
Copyright © 2020-2023  润新知