UI控件--AlertDialog

最简单的Alertdialog

  • 效果图
    UI控件--AlertDialog_第1张图片
  • 代码如下:
                AlertDialog.Builder builder=new AlertDialog.Builder(this);
                builder.setTitle("请注意!!!");
                builder.setIcon(R.mipmap.ic_launcher);
                builder.setMessage("你确定要删除吗?");
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "OK", Toast.LENGTH_SHORT).show();
                    }
                });

                builder.setNegativeButton("cancal", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "cancal", Toast.LENGTH_SHORT).show();
                    }
                });
                builder.show();

带单选框的AlertDialog

注意无论是带单选框还是带多选框的弹窗,都不可设置setMessage方法,若调用此方法,会导致message内容覆盖选项

  • 效果图如下
    UI控件--AlertDialog_第2张图片
  • 代码如下
                final String[] strings={"女","男"};
                AlertDialog.Builder builder=new AlertDialog.Builder(this);
                builder.setTitle("请注意!!!");
                builder.setIcon(R.mipmap.ic_launcher);
                builder.setSingleChoiceItems(strings, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, strings[which], Toast.LENGTH_SHORT).show();
                    }
                });
                builder.show();

带多选框的Alertdialog

  • 效果图如下
    UI控件--AlertDialog_第3张图片
  • 代码如下:
                final String[] strings={"斗地主","LOL","打麻将","下象棋"};
                final boolean[] isOn=new boolean[strings.length];
                AlertDialog.Builder builder=new AlertDialog.Builder(this);
                builder.setTitle("请注意!!!");
                builder.setIcon(R.mipmap.ic_launcher);
                builder.setMultiChoiceItems(strings, isOn, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        isOn[which]=isChecked;
                    }
                });

                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String str="";
                        for (int i=0;i<strings.length;i++){
                            if (isOn[i]){
                                str+=strings[i];
                            }
                        }
                        Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
                    }
                });

                builder.show();

自定义Dialog

  • 效果图如下:

  • 自定义布局如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">

    <TextView  android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Text" android:id="@+id/textView1111" />

    <TextView  android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Text" android:id="@+id/textView2222" />
</LinearLayout>
  • 代码如下
                Dialog dialog1=new Dialog(this);
                View view1=LayoutInflater.from(this).inflate(R.layout.my_layout,null);
                TextView textView= (TextView) view1.findViewById(R.id.textView1111);
                textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(MainActivity.this, "我是第一个TextView", Toast.LENGTH_SHORT).show();
                    }
                });
                TextView textView1= (TextView) view1.findViewById(R.id.textView2222);
                textView1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(MainActivity.this, "我是第2个TextView", Toast.LENGTH_SHORT).show();
                    }
                });
                dialog1.setTitle("自定义的弹窗");
                dialog1.setContentView(view1);
                dialog1.show();

你可能感兴趣的:(UI,dialog,控件)