Example 1 of AlertDialog in Android (warning dialog box): androidalertdialog

Source: Internet
Author: User

Example 1 of AlertDialog in Android (warning dialog box): androidalertdialog

In Android development, we often need to pop up some dialogs on the Android interface, such as asking users or asking users to choose. These functions are called the Android Dialog box. The AlertDialog implementation method is the builder mode. The following is the most common warning dialog box that appears when you uninstall an application, for example:

Example code of layout interface:

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 3 android: orientation = "vertical" android: layout_width = "match_parent" 4 android: layout_height = "match_parent"> 5 <Button 6 android: text = "Uninstall" 7 android: layout_width = "match_parent" 8 android: layout_height = "wrap_content" 9 android: onClick = "show" 10 android: id = "@ + id/button"/> 11 </LinearLayout>

Java implementation code:

1 import android. content. dialogInterface; 2 import android. OS. bundle; 3 import android. support. annotation. nullable; 4 import android. support. v7.app. alertDialog; 5 import android. support. v7.app. appCompatActivity; 6 import android. view. view; 7 import android. widget. toast; 8/** 9 * Created by panchengjia on 2016/11/21. 10 */11 public class AlertDialogDemo extends AppCompatActivity {12 @ Override13 protected void onCreate (@ Nullable Bundle savedInstanceState) {14 super. onCreate (savedInstanceState); 15 setContentView (R. layout. alterdialog); 16} 17 public void show (View v) {18 // instantiate builder 19 AlertDialog. builder builder = new AlertDialog. builder (this); 20 // set the title of the warning dialog box 21 builder. setTitle ("Uninstall"); 22 // set the image displayed with the warning 23 // builder. setIcon (android. r. drawable. ic_dialog_alert); 24 // set the prompt information in the warning dialog box 25 builder. setMessage ("are you sure you want to uninstall"); 26 // set the "front" button, and click event 27 builder. setPositiveButton ("OK", new DialogInterface. onClickListener () {28 @ Override29 public void onClick (DialogInterface dialog, int which) {30 Toast. makeText (AlertDialogDemo. this, "click OK", Toast. LENGTH_SHORT ). show (); 31} 32}); 33 // set the "reverse" button and click event 34 builder. setNegativeButton ("cancel", new DialogInterface. onClickListener () {35 @ Override36 public void onClick (DialogInterface dialog, int which) {37 Toast. makeText (AlertDialogDemo. this, "click the cancel button", Toast. LENGTH_SHORT ). show (); 38} 39}); 40 // set the "neutral" button and click event 41 builder. setNeutralButton ("wait and see", new DialogInterface. onClickListener () {42 @ Override43 public void onClick (DialogInterface dialog, int which) {44 Toast. makeText (AlertDialogDemo. this, "click the neutral button", Toast. LENGTH_SHORT ). show (); 45} 46}); 47 // display Dialog Box 48 builder. show (); 49} 50}

 

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.