A brief analysis of the powerful dialog_android in Android

Source: Internet
Author: User
Tags stub

Android often uses the dialog (pop-up effect), and is very powerful, can simulate the N-type pop-up effect. As shown in the figure:

Here's a small example to show you the powerful dialog in Android. The code has written a detailed comment, and the reader might as well try to knock it manually.

Of course, due to the time of the relationship, there are individual features have not been completed, some of the first few display effects may also have bugs, the code also has a lot to optimize the place. I hope everyone is enthusiastic to point out.

Here's the code (a little bit longer OH):

Copy Code code as follows:

Package com.chaoyang.activity;

Import java.util.ArrayList;

Import android.app.Activity;
Import Android.app.AlertDialog;
Import Android.app.ProgressDialog;
Import Android.content.DialogInterface;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.Button;


public class Mainactivity extends activity {
/** called the activity is a. */

Final string[] items={"John", "Dick", "Harry", "Zhao Liu", "a Cat", "A Dog"};
int Msinglechoiceid =-1;
ArrayList <integer>multichoiceid = new ArrayList <Integer> ();
Private ProgressDialog mprogressdialog = null;
private static final int max_progress = 100;
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
/*start set up the click Listener Object for those buttons *
Buttononcliklisten Listen =new buttononcliklisten ();
Button Btnconfim = (button) This.findviewbyid (R.id.btnconfim);
Button btncheck = (button) This.findviewbyid (R.id.btncheck);
Button Btncustom = (button) This.findviewbyid (R.id.btncustom);
Button btnlist = (button) This.findviewbyid (r.id.btnlist);
Button Btnmore = (button) This.findviewbyid (R.id.btnmore);
Button Btnprogressbar = (button) This.findviewbyid (R.id.btnprogressbar);
Button Btnread = (button) This.findviewbyid (R.id.btnread);
Button Btnridio = (button) This.findviewbyid (R.id.btnridio);

Btnconfim.setonclicklistener (listen);
Btncheck.setonclicklistener (listen);
Btnconfim.setonclicklistener (listen);
Btnlist.setonclicklistener (listen);
Btnmore.setonclicklistener (listen);
Btnprogressbar.setonclicklistener (listen);
Btnread.setonclicklistener (listen);
Btnridio.setonclicklistener (listen);

/* END * *

}
Define a public method, pop-up message
private void Showdialogmsg (String msg) {
New Alertdialog.builder (Mainactivity.this)
. Setmessage (msg)
. Show ();
}
Defines an inner class, inherits to Onclicklistener, and deals specifically with button-click events (object-oriented, flexible)
Private Final class Buttononcliklisten implements View.onclicklistener
{

@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Button button = (button) v;//Receive clicked button object
Alertdialog.builder Builder =new Alertdialog.builder (mainactivity.this);//define a pop-up object
Builder.seticon (R.DRAWABLE.AA);//Set Icon for pop-up box

Switch (V.getid ())
{
Case R.ID.BTNCONFIM:
Builder.settitle ("OK commit?") ");//Set the title of the pop-up box
Builder.setpositivebutton (OK), new Dialoginterface.onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
Showdialogmsg ("You have chosen to determine");
}
});
Builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {

@Override
public void OnClick (Dialoginterface dialog, int which) {
Showdialogmsg ("You chose to cancel");
}
});

Break
Case R.id.btnmore:
Builder.settitle ("Evaluation");//title
Builder.setmessage ("Talk about Lee Hyo Ri in Your Mind");/popup message
Builder.setpositivebutton ("Sexy", new Dialoginterface.onclicklistener () {

@Override
public void OnClick (Dialoginterface dialog, int which) {
TODO auto-generated Method Stub
Showdialogmsg ("You think she's hot");
}
});
Builder.setneutralbutton ("Pretty", new Dialoginterface.onclicklistener () {

@Override
public void OnClick (Dialoginterface dialog, int which) {
TODO auto-generated Method Stub
Showdialogmsg ("You think she's beautiful");
}
});
Builder.setnegativebutton ("coquettish", new Dialoginterface.onclicklistener () {

@Override
public void OnClick (Dialoginterface dialog, int which) {
TODO auto-generated Method Stub
Showdialogmsg ("You think she is very coquettish");
}
});
Break
Case R.id.btnlist:
Builder.settitle ("list box");
Builder.setitems (items, new Dialoginterface.onclicklistener () {

@Override
public void OnClick (Dialoginterface dialog, int which) {
TODO auto-generated Method Stub
Showdialogmsg ("You have selected the person with id" +which+ ", Name:" +items[which]);
}
});
Break
Case R.id.btnridio:
Msinglechoiceid =-1;
Builder.settitle ("Single Selection Box");
Builder.setsinglechoiceitems (items, 0, new Dialoginterface.onclicklistener () {

@Override
public void OnClick (Dialoginterface dialog, int which) {
TODO auto-generated Method Stub
Msinglechoiceid=which;
Showdialogmsg ("You have selected the ID is" +msinglechoiceid+ ", the name is:" +items[which]);
}
});
Builder.setpositivebutton (OK), new Dialoginterface.onclicklistener () {

@Override
public void OnClick (Dialoginterface dialog, int which) {
TODO auto-generated Method Stub
if (msinglechoiceid>-1)
{
Showdialogmsg ("Your final choice is" +items[msinglechoiceid]);
}else
{
Showdialogmsg ("Your final choice is" +items[0]);
}
}
});
Builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {

@Override
public void OnClick (Dialoginterface dialog, int which) {
TODO auto-generated Method Stub

}
});
Break
Case R.id.btncheck:
Multichoiceid.clear (); Empty the collection first
Builder.settitle ("multiple options");
Builder.setmultichoiceitems (Items,new Boolean[]{false,false,false,false,false,false}, new Dialoginterface.onmultichoiceclicklistener () {

@Override
public void OnClick (Dialoginterface dialog, int which, Boolean ischecked) {
TODO auto-generated Method Stub
if (ischecked)
{
Multichoiceid.add (which);
Showdialogmsg ("You have selected" +items[which]);
}else
{
Multichoiceid.remove (which);
}

}
});
Builder.setpositivebutton (OK), new Dialoginterface.onclicklistener () {

@Override
public void OnClick (Dialoginterface dialog, int which) {
TODO auto-generated Method Stub

String str= "";
for (int i = 0;i<multichoiceid.size (); i++)
{
Str+=items[multichoiceid.get (i)]+ ",";
}
Remove the last ","
if (Str.length () >0) {
str = str.substring (0,str.length ()-1);
}
Showdialogmsg ("One of the altogether selected has" +str);
}
});
Builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {

@Override
public void OnClick (Dialoginterface dialog, int which) {
TODO auto-generated Method Stub

}
});
Break
Case R.id.btnprogressbar:
Mprogressdialog =new ProgressDialog (mainactivity.this);
Mprogressdialog.settitle ("Progress bar Window");
Mprogressdialog.setprogressstyle (progressdialog.style_horizontal);//Set style
Mprogressdialog.setmax (max_progress);//Set maximum value
Mprogressdialog.setbutton (OK), new Dialoginterface.onclicklistener () {
public void OnClick (dialoginterface dialog, int whichbutton) {
Click here to add the logic after
}
});
Mprogressdialog.setbutton2 ("Cancel", new Dialoginterface.onclicklistener () {
public void OnClick (dialoginterface dialog, int whichbutton) {
Click here to add the logic after
}
});
Mprogressdialog.show ();
New Thread (). Start ();
Return
Case R.id.btncustom:

Break
Case R.id.btnread:

Break
}

Builder.create (). Show ();//Create and display a pop-up box


}


}


}


Part of the layout file, relatively simple:
Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "Vertical"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
>
<textview
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "@string/hello"
/>
<button
Android:id= "@+id/btnconfim"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "OK/Cancel"
/>
<button
Android:id= "@+id/btnmore"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "multiple message prompts"
/>
<button
Android:id= "@+id/btnlist"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "list Box"
/>
<button
Android:id= "@+id/btnprogressbar"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "Progress bar"
/>
<button
Android:id= "@+id/btnridio"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "Radio Box"
/>
<button
Android:id= "@+id/btncheck"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:text= "Multi-selection box"
/>
<button
Android:id= "@+id/btncustom"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "Custom Layout"
/>
<button
Android:id= "@+id/btnread"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "Read Progress Box"
/>
</LinearLayout>

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.