Basic use of alertdialog for Android

Frankly, I don't use alertdialog much in my work, because the style of alertdialog is relatively fixed and rigid. In order to match the overall design of the app, I usually use a custom dialog, which can only be used when the requirements are not high. However, as one of the basic controls of Android, it is very necessary to master it. There are so many relevant information on the Internet that there are several pages after a search. But I decided to write my own blog.

First, let's take a look at the general creation order of alertdialog. Slightly different from textview and button controls, alertdialog does not directly call various methods after initialization (findviewbyid). Think about the use scenario of alertdialog. Unlike textview and button controls, it is usually fixed on the interface, but it will be triggered at a certain time (for example, the user clicks a button or disconnects the network). Therefore, alertdialog does not need to be created in the layout file, but constructs the contents such as title, icon and button through the constructor (alertdialog. Builder) in the code.

Alertdialog. Builder also has a show method to display dialog boxes, so steps 4 and 5 above can be simplified to one step.

Next, let's simply create several common alertdialog. Create a new project, place different buttons on mainactivity, and click the button to pop up the corresponding dialog box.

The prompt dialog box should be the most common alertdialog, with prompt text at the top and buttons such as cancel and OK at the bottom. Combined with the previous creation steps, I believe the following code is not difficult to understand.

    /**
     * 提示对话框
     *
     * @param view
     */
    public void tipClick(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("问题:");
        builder.setMessage("请问你满十八岁了吗?");
        builder.setIcon(R.mipmap.ic_launcher_round);
        //点击对话框以外的区域是否让对话框消失
        builder.setCancelable(true);
        //设置正面按钮
        builder.setPositiveButton("是的",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog,int which) {
                Toast.makeText(context,"你点击了是的",Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        });
        //设置反面按钮
        builder.setNegativeButton("不是","你点击了不是",Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        });
        //设置中立按钮
        builder.setNeutralButton("保密","你选择了保密",Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        });
        AlertDialog dialog = builder.create();
        //对话框显示的监听事件
        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                Log.e(TAG,"对话框显示了");
            }
        });
        //对话框消失的监听事件
        dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                Log.e(TAG,"对话框消失了");
            }
        });
        //显示对话框
        dialog.show();
    }
@H_301_28@

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>