美文网首页
AlertDialog 实用API及其示例

AlertDialog 实用API及其示例

作者: 我们都很努力着 | 来源:发表于2020-05-22 17:09 被阅读0次

本文将介绍常用AlertDialog Api及其常用AlertDialog示例

常用API

常用方法

方法定义 方法说明
setIcon(int iconId) 设置对话框的图标
setCancelable(boolean cancelable) 设置对话框是否可取消
setTitle(CharSequence title) 设置对话框的标题
setMessage(CharSequence message) 设置对话框主体内容
setAdapter(ListAdapter adapter, OnClickListener listener) 设置一个adapter
setSingleChoiceItems() 设置单选
setMultiChoiceItems() 设置多选

设置宽满屏

Window dialogWindow = alert.getWindow();
WindowManager.LayoutParams p = dialogWindow.getAttributes();
p.width = WindowManager.LayoutParams.MATCH_PARENT; 
dialogWindow.setAttributes(p);

AlertDialog设置宽满屏时显示却有margin属性

alert.getWindow().setBackgroundDrawable(null); 

设置居中

alert.getWindow().setGravity(Gravity.CENTER);

Alterdialog点击空白处不消失的方法

alert.setCanceledOnTouchOutside(false);

示例

默认普通对话框

AlertDialog dialog = new AlertDialog
                .Builder(this)
    .setTitle("普通对话框")
    .setIcon(R.mipmap.ic_launcher)
    .setNegativeButton("取消", null)
    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) { 
            Toast.makeText(getApplicationContext(), "确定", Toast.LENGTH_SHORT).show();
        }
    }).setNeutralButton("默认", null)
    .setMessage("确认删除?").create();
dialog.show();

显****示****Item****对话****框

final CharSequence[] items = {"北京", "上海", "广州"};
AlertDialog dialog = new AlertDialog
                .Builder(this)
    .setTitle("显示Item对话框")
    .setItems(items, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(getApplicationContext(), items[which], Toast.LENGTH_SHORT).show();
        }
    }).create();
dialog.show();

单选列表对话框 setSingleChoiceItems

final CharSequence[] items = { "北京", "上海", "广州" };
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("请选择以下城市");
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        CharSequence sequence = items[which];
        Toast.makeText(getApplicationContext(), "select " + sequence, Toast.LENGTH_SHORT).show();
    }
});
AlertDialog dialog = builder.create();
dialog.show();

多选对话框setMultiChoiceItems

final CharSequence[] items = {"北京", "上海", "广州"};
AlertDialog dialog = new AlertDialog.Builder(this).setTitle("多选对话框").setIcon(android.R.drawable.sym_def_app_icon)
    .setNegativeButton("取消", null).setPositiveButton("确定", null)
    .setMultiChoiceItems(items, null, new DialogInterface.OnMultiChoiceClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which, boolean isChecked) {
            Toast.makeText(getApplicationContext(), "select " + items[which], Toast.LENGTH_SHORT).show();
        }
    }).create();
dialog.show();

自定义Adapter

final String[] items = {"北京", "上海", "广州"};
ArrayAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
AlertDialog dialog = new AlertDialog.Builder(this).setTitle("自定义Adapter").setIcon(android.R.drawable.sym_def_app_icon)
    .setAdapter(adapter, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(getApplicationContext(), "select " + items[which], Toast.LENGTH_SHORT).show();
        }
    }).create();
dialog.show();

自定义View

String name = "自定义View";
        View view = LayoutInflater.from(this).inflate(android.R.layout.simple_list_item_1, null);
        ((TextView)view.findViewById(android.R.id.text1)).setText(name);
        AlertDialog dialog = new AlertDialog
                .Builder(this)
                .setTitle(name)
                .setIcon(android.R.drawable.sym_def_app_icon)
                .setView(view)
                .create();
        dialog.show();

相关文章

  • AlertDialog 实用API及其示例

    本文将介绍常用AlertDialog Api及其常用AlertDialog示例 常用API 常用方法 方法定义方法...

  • 如何使用AlertDialog和ProgressDialog

    AlertDialog代码示例 AlertDialog是一个带有确定取消按钮的系统弹窗,因为是系统控件,不需要在l...

  • k8s的一些配置

    查看api接口版本:kubectl api-versions GitHub示例

  • 获取手机归属地免费API

    手机归属地免费API 1.百度免费api 返回示例 2.淘宝api 只能获取到省 返回示例 3.百付宝API js...

  • Go - Micro微服务框架实践 - API(五)

    micro api是api网关 概观 安装 运行 使用ACME 设置TLS证书 设置命名空间 例子运行示例调用示例...

  • 接文档

    初始化APi 说明文字 请求参数 响应参数 请求示例 响应示例 异常示例 库存查询APi 说明文字 请求参数 响应参数

  • JAVA API-day01

    A API 字符串的基本操作 StringBuilder及其API

  • pydub及其示例

    该库是比较好的音频处理库,适用于音频切分等功能。 Windows下安装: 如果使用非wav格式的文件,通过ffmp...

  • ★36.Gson

    简介 GitHub地址:Gson API文档:Gson API 基本类型示例 对象示例 代码 一些细节 最好使用p...

  • vued单页面组件中导出excel表格方法

    示例一 在vue单页面组件中template中写法示例 api.js中写法示例 在script标签中写法示例 示例...

网友评论

      本文标题:AlertDialog 实用API及其示例

      本文链接:https://www.haomeiwen.com/subject/vpdlnhtx.html