Android Alertdialog (Implementation warning dialog box) _android

Source: Internet
Author: User

In Android development, we often need to pop up some dialog boxes on the Android interface, such as asking the user or letting the user choose. These features we call it the Android Dialog dialog box, Alertdialog implementation method for Builder mode. Below we simulate the most common warning dialog box that pops up when uninstalling an application, as shown in the following figure:

Layout Layout Interface code example:

<?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 ">
  <button
    android:text= Uninstall"
    android:layout_width= "Match_parent"
    android: layout_height= "Wrap_content"
    android:onclick= "show"
    android:id= "@+id/button"/>
</ Linearlayout>

Java implementation code:

Import Android.content.DialogInterface;
Import Android.os.Bundle;
Import android.support.annotation.Nullable;
Import Android.support.v7.app.AlertDialog;
Import android.support.v7.app.AppCompatActivity;
Import Android.view.View;
Import Android.widget.Toast;
 /** * Created by Panchengjia on 2016/11/21. */public class Alertdialogdemo extends appcompatactivity {@Override protected void onCreate (@Nullable Bundle Savedin
    Stancestate) {super.oncreate (savedinstancestate);
  Setcontentview (R.layout.alterdialog);
    } public void Show (View v) {//instantiated builder Alertdialog.builder Builder = new Alertdialog.builder (this);
    Sets the caption Builder.settitle ("Uninstall") of the warning dialog box; Set the picture//Builder.seticon (Android) that the warning displays.
    R.drawable.ic_dialog_alert);
    Set the prompt message for the warning dialog box Builder.setmessage ("OK Uninstall"); Set the "Front" button, and click event Builder.setpositivebutton (OK), new Dialoginterface.onclicklistener () {@Override public void OnClick (Dialoginterface dialog, int which) {Toast.maketExt (Alertdialogdemo.this, "Click the OK button", Toast.length_short). Show ();
    }
    }); Set the "Back" button, and click event Builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {@Override public void OnClick (Dialoginterface dialog, int which) {Toast.maketext (Alertdialogdemo.this, "Click the Cancel button", Toast.length_short
      ). Show ();
    }
    });  Set "neutral" button, and click event Builder.setneutralbutton ("Wait and see", New Dialoginterface.onclicklistener () {@Override public void OnClick (Dialoginterface dialog, int which) {Toast.maketext (Alertdialogdemo.this, "clicked Neutral button", Toast.length_shor
      T). Show ();
    }
    });
  Display dialog box Builder.show ();
 }
}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

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.