笔记8 Dialog常见类型及常用属性 自定义dialog






1、确认对话

private void showConfirmDialog() {//确认对话框

    AlertDialog.Builder builder = new AlertDialog.Builder(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(MainActivity.this,"确定",Toast.LENGTH_LONG).show();
        }
    });
    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(MainActivity.this,"取消",Toast.LENGTH_LONG).show();
        }
    });

    AlertDialog dialog = builder.create();//获取dialog
    dialog.show();//显示dialog
}

2、单选按钮对话框

private void showSingleDialog() {//单选按钮对话框

    final String [] single_list = {"男","女"};

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("单选按钮对话框");//设置标题
    builder.setIcon(R.mipmap.ic_launcher);//设置图标
    //builder.setSingleChoiceItems(参数1,参数2,参数3)
    // 第一个参数:是单选按钮的数据
    // 第二个参数:已经选中的条目
    // 第三个参数:监听
    builder.setSingleChoiceItems(single_list, 0, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            String str = single_list[which];
            Toast.makeText(MainActivity.this,"您选择的是:"+str,Toast.LENGTH_SHORT).show();
        }
    });

    AlertDialog dialog = builder.create();//获取dialog
    dialog.show();//显示dialog
}

3、多选按钮对话框

private void showMultiDialog() {//多选按钮对话框

    final String [] multi_list = {"1","2","3"};

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("多选按钮对话框");//设置标题
    builder.setIcon(R.mipmap.ic_launcher);//设置图标
    //builder.setSingleChoiceItems()
    // 第一个参数:是多选按钮的数据
    // 第二个参数:已经选中的items
    // 第三个参数:监听
    builder.setMultiChoiceItems(multi_list, null, new DialogInterface.OnMultiChoiceClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which, boolean isChecked) {
            String str = multi_list[which];
            if(isChecked){
                Toast.makeText(MainActivity.this,"您选择的是:"+str,Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(MainActivity.this,"您取消选择的是:"+str,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();//显示dialog
}

4、列表对话框

private void showItemsDialog() {//列表对话框

    final String [] items_list = {"1","2","3"};

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("列表对话框");//设置标题
    builder.setIcon(R.mipmap.ic_launcher);//设置图标
    //builder.setSingleChoiceItems()
    // 第一个参数:是列表的数据
    // 第二个参数:已经选中的条目
    // 第三个参数:监听
    builder.setItems(items_list, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            String str = items_list[which];
            Toast.makeText(MainActivity.this,"您选择的是:"+str,Toast.LENGTH_SHORT).show();
        }
    });

    AlertDialog dialog = builder.create();//获取dialog
    dialog.show();//显示dialog
}

5、自定义对话框


private void showDIYDialog() {//列表对话框
    LayoutInflater inflater = LayoutInflater.from(this);
    View view = inflater.inflate(R.layout.dialog_layout,null);

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("列表对话框");//设置标题
    builder.setIcon(R.mipmap.ic_launcher);//设置图标
    builder.setView(view);//设置自定义样式布局
    AlertDialog dialog = builder.create();//获取dialog
    dialog.show();//显示dialog
}

布局文件代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="输入内容..."
        android:layout_weight="1"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="提交"
        android:layout_marginLeft="10dip"
        />
    </LinearLayout>
   <ImageView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginTop="5dip"
       android:src="@mipmap/ic_launcher"
       />
</LinearLayout>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值