"Android" uses builder to create Alertdialog

Source: Internet
Author: User

Briefly

I feel very friendly when I call Alertdialog.builder directly in the activity to create a dialog without having to create a class alone. Consider that the actual Android app dialog need to complete the task is not too much, so this method should be able to achieve 90% of the demand, compared to the official Android document with serious to create a dialog box is simple, say not much, look at this mode it.

First, we need a dialog box layout

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" 256DP "android:layout_height=" 141DP "android:orientation=" vertical "> <linearlayout android:layout_width=" match_parent "Android:layo            ut_height= "92DP" android:gravity= "center" android:orientation= "vertical" > <textview Android:id= "@+id/dialog_title" android:layout_width= "wrap_content" android:layout_height= "Wrap_conte NT "android:layout_gravity=" center "android:layout_margintop=" 10DP "Android:linespacingex Tra= "3DP" android:linespacingmultiplier= "1.2" android:textcolor= "#333333" Android:textsiz            E= "15SP" android:visibility= "Gone"/> <textview android:id= "@+id/dialog_message" Android:layout_width= "Wrap_conTent "android:layout_height=" wrap_content "android:layout_gravity=" center "android:layout _marginleft= "15DP" android:layout_marginright= "15DP" android:layout_margintop= "3DP" Andro            Id:linespacingextra= "3DP" android:linespacingmultiplier= "1.2" android:textcolor= "#333333"        Android:textsize= "15sp"/> </LinearLayout> <linearlayout android:layout_width= "Match_parent" android:layout_height= "40DP" android:gravity= "center" android:orientation= "Horizontal" > </linea Rlayout></linearlayout>

Locate the layout in the resource file using the Getlayoutinflater (). Inflate () method

LinearLayout layout=(LinearLayout) getLayoutInflater().inflate(R.layout.dialog,null);

By the way, set R.string.dlg_title the text, and R.string.dlg_message is the string resources to add their own items

TextView dialogTile=layout.findViewById(R.id.dialog_title);TextView dialogMessage=layout.findViewById(R.id.dialog_message);dialogTile.setText(R.string.dlg_title);dialogMessage.setText(R.string.dlg_message);

is here to add

Build this dialog box

 new AlertDialog.Builder(MainActivity.this).setView(layout)                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {                            @Override                            public void onClick(DialogInterface dialogInterface, int i) {                                /*                                点击确定按钮要做的事                                */                            }                        })                        .setNegativeButton("取消",null)//因为取消键只是关闭,所以不设监听                        .setCancelable(true)//可以点对话框外部关闭对话框                        .create()                        .show();

The above three pieces of code are written directly in the activity, do not create a new class, write it is also very short, feel good.

"Android" uses builder to create Alertdialog

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.