Five types of pop-up dialogs commonly used by Android

Source: Internet
Author: User

A small example of a common dialog box used in Android development, there are five types of dialogs: normal pop-up dialog, single-selection dialog, multi-Select Dialog, Input dialog box and progress Bar Style dialog box:

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical" >


<button
Android:id= "@+id/common_dialog"
Android:layout_width= "Match_parent"
android:layout_height= "40DP"
android:text= "Normal dialog box"
Android:textsize= "16SP"
android:layout_margintop= "10DP"/>


<button
Android:id= "@+id/radio_dialog"
Android:layout_width= "Match_parent"
android:layout_height= "40DP"
android:text= "Radio dialog box"
Android:textsize= "16SP"
android:layout_margintop= "10DP"/>


<button
Android:id= "@+id/check_dialog"
Android:layout_width= "Match_parent"
android:layout_height= "40DP"
android:text= "Multiple selection dialog box"
Android:textsize= "16SP"
android:layout_margintop= "10DP"/>


<button
Android:id= "@+id/input_dialog"
Android:layout_width= "Match_parent"
android:layout_height= "40DP"
android:text= "Enter Text dialog box"
Android:textsize= "16SP"
android:layout_margintop= "10DP"/>


<button
Android:id= "@+id/progress_dialog"
Android:layout_width= "Match_parent"
android:layout_height= "40DP"
android:text= "Progress bar dialog box"
Android:textsize= "16SP"
android:layout_margintop= "10DP"/>


</LinearLayout>

The following is a simple layout of the input content Activity_input.xml

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:id= "@+id/linearlayout1"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical" >


<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/hello_world"/>


<edittext
Android:id= "@+id/uname"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"/>


<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/hello_world"/>


<edittext
Android:id= "@+id/upass"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"/>


</LinearLayout>

Code and comments:

public class Mainactivity extends Activity implements Onclicklistener {
/** Single Box simulation Title University * *
Private final static int checked_enu = 0;
/** Single Box simulation title High School */
Private final static int checked_sel = 1;
/** Single Box simulation title Junior * *
Private final static int checked_chu = 2;
/** Check button status is Select All */
Private boolean[] checked = {true, True, True, false};
/** the progress value of the simulation */
private int progressnumber;
/** Progress dialog Box */
Private ProgressDialog ProgressDialog;
/** corresponding Button */
Private Button commonbtn, radiobtn, Checkbtn, inputbtn, progressbtn;


@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Initviews ();
Initlisteners ();
}


/** Initializing UI Controls */


private void Initviews () {
THIS.COMMONBTN = (Button) Findviewbyid (R.id.common_dialog);
THIS.RADIOBTN = (Button) Findviewbyid (R.id.radio_dialog);
THIS.CHECKBTN = (Button) Findviewbyid (R.id.check_dialog);
THIS.INPUTBTN = (Button) Findviewbyid (R.id.input_dialog);
THIS.PROGRESSBTN = (Button) Findviewbyid (R.id.progress_dialog);
}


/** Register button Listener event */
private void Initlisteners () {
This.commonBtn.setOnClickListener (this);
This.radioBtn.setOnClickListener (this);
This.checkBtn.setOnClickListener (this);
This.inputBtn.setOnClickListener (this);
This.progressBtn.setOnClickListener (this);
}


/** General dialog Box */
Private Dialog Buildalertdialog () {
Alertdialog.builder Builder = new Alertdialog.builder (this);
Builder.seticon (R.drawable.ic_launcher);
Builder.settitle ("dialog box");
Builder.setmessage ("Your password is incorrect!!");


ImageView ImageView = new ImageView (this);
Imageview.setimageresource (R.DRAWABLE.MM1);
/** Setting the background image */
Builder.setview (ImageView);
/** Left button */
Builder.setpositivebutton ("OK", new Dialoginterface.onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
Settitle ("You clicked on the left OK button!");
}
});
/** middle Button */
Builder.setneutralbutton ("Details", new Dialoginterface.onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
Settitle ("You clicked the Middle Details button!");
}
});
/** Right Button */
Builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
TODO auto-generated Method Stub
Settitle ("You clicked on the right Cancel button!");
}
});
return Builder.create ();
}


/** radio button pop-up box */
Private Dialog Buildalertdialog_radio () {
Alertdialog.builder Builder = new Alertdialog.builder (this);
Builder.seticon (R.drawable.ic_launcher);
Builder.settitle ("dialog box");
/** radio button, default high School is selected */
Builder.setsinglechoiceitems (new string[] {"University", "High School", "Junior High", "Primary School"}, 1, new Dialoginterface.onclicklistener () {


@Override
public void OnClick (Dialoginterface dialog, int which) {
TODO auto-generated Method Stub
Switch (which) {
Case Checked_enu:
Settitle ("university");
Break
Case Checked_sel:
Settitle ("High School");
Break
Case Checked_chu:
Settitle ("Junior High School");
Break
Default
Settitle ("primary School");
Break
}
}
});


Builder.setpositivebutton ("OK", new Dialoginterface.onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
Settitle ("You clicked on the left OK button!");
}
});
Builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
Settitle ("You clicked on the right Cancel button!");
}
});
return Builder.create ();
}


/** can select the button popup */
Private Dialog Buildalertdialog_checkbox () {
Alertdialog.builder Builder = new Alertdialog.builder (this);
Builder.seticon (R.drawable.ic_launcher);
Builder.settitle ("dialog box");
/** Check button */
Builder.setmultichoiceitems (new string[] {"University", "High School", "Junior High School", "Primary School"}, checked, new Dialoginterface.onmultichoiceclicklistener () {


@Override
public void OnClick (Dialoginterface dialog, int which, Boolean isChecked) {
Settitle ("which=" + which + "-----" + "ischecked=" + isChecked);
}
});


Builder.setpositivebutton ("OK", new Dialoginterface.onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
Settitle ("You clicked the OK button!");
}
});
Builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
TODO auto-generated Method Stub
Settitle ("You clicked the Cancel button!");
}
});
return Builder.create ();
}


/** contains pop-up boxes that can enter text */
Private Dialog Buildalertdialog_input () {
Alertdialog.builder Builder = new Alertdialog.builder (this);
Builder.seticon (R.drawable.ic_launcher);
Builder.settitle ("dialog box");
Layoutinflater Inflater = Layoutinflater.from (this);
Builder.setview (inflater.inflate (r.layout.activity_input, null));
Builder.setpositivebutton ("OK", new Dialoginterface.onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
Settitle ("You clicked the OK button!");
}
});
Builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
Settitle ("You clicked the Cancel button!");
}
});
return Builder.create ();
}


/** Progress dialog Box */
Private Dialog buildalertdialog_progress () {
ProgressDialog = new ProgressDialog (this);
Progressdialog.settitle ("Progress bar");
Progressdialog.setmessage ("Downloading ......");
/** progress bar Style */
Progressdialog.setprogressstyle (progressdialog.style_horizontal);
/** Blur effect */
Progressdialog.setindeterminate (FALSE);
return progressdialog;
}


/** update progress every 0.3 seconds */
public void UpdateProgress () {
New Thread () {
@Override
public void Run () {
try {
while (Progressnumber <= 100) {
Progressdialog.setprogress (progressnumber++);
Thread.Sleep (300);
Super.run ();
}
/** Download finished, close the download box */
Progressdialog.cancel ();
} catch (Interruptedexception e) {
E.printstacktrace ();
}
}
}.start ();
}


@Override
public void OnClick (View v) {
Switch (V.getid ()) {
Case R.id.common_dialog:
Buildalertdialog (). Show ();
Break
Case R.id.radio_dialog:
Buildalertdialog_radio (). Show ();
Break
Case R.id.check_dialog:
Buildalertdialog_checkbox (). Show ();
Break
Case R.id.input_dialog:
Buildalertdialog_input (). Show ();
Break
Case R.id.progress_dialog:
Buildalertdialog_progress (). Show ();
UpdateProgress ();
Break
Default
Break
}
}
}

Five types of pop-up dialogs commonly used by Android

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.