Five common pop-up dialog boxes for Android and five types of dialog boxes for android

Source: Internet
Author: User

Five common pop-up dialog boxes for Android and five types of dialog boxes for android

A small example of commonly used dialogs in Android development. There are five kinds of dialogs: normal pop-up dialog box, single-choice dialog box, multiple-choice dialog box, 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 = "single choice 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 = "Multi-choice 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 = "input 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 region simulated title University */
Private final static int CHECKED_ENU = 0;
/** Single region simulated title high school */
Private final static int CHECKED_SEL = 1;
/** Single-tenant simulated Junior High School title */
Private final static int CHECKED_CHU = 2;
/** Select all button status */
Private boolean [] checked = {true, false };
/** Simulated progress value */
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 ();
}


/** Initialize the UI control */


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 to listen to events */
Private void initListeners (){
This. commonBtn. setOnClickListener (this );
This. radioBtn. setOnClickListener (this );
This. checkBtn. setOnClickListener (this );
This. inputBtn. setOnClickListener (this );
This. progressBtn. setOnClickListener (this );
}


/** Normal 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 );
/** Set the background image */
Builder. setView (imageView );
/** Buttons on the left */
Builder. setPositiveButton ("OK", new DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
SetTitle ("click the OK button on the left! ");
}
});
/** Intermediate button */
Builder. setNeutralButton ("details", new DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
SetTitle ("You are clicking the intermediate details button! ");
}
});
/** Buttons on the right */
Builder. setNegativeButton ("cancel", new DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
// TODO Auto-generated method stub
SetTitle ("click the cancel button on the right! ");
}
});
Return builder. create ();
}


/** Single choice 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 ");
/** Single-choice button, selected by default for high school */
Builder. setSingleChoiceItems (new String [] {"", ""}, 1, new DialogInterface. OnClickListener (){


@ Override
Public void onClick (DialogInterface dialog, int which ){
// TODO Auto-generated method stub
Switch (which ){
Case CHECKED_ENU:
SetTitle (" ");
Break;
Case CHECKED_SEL:
SetTitle ("High School ");
Break;
Case CHECKED_CHU:
SetTitle ("Junior High School ");
Break;
Default:
SetTitle ("Elementary School ");
Break;
}
}
});


Builder. setPositiveButton ("OK", new DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
SetTitle ("click the OK button on the left! ");
}
});
Builder. setNegativeButton ("cancel", new DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
SetTitle ("click the cancel button on the right! ");
}
});
Return builder. create ();
}


/** You can select multiple buttons in the pop-up box */
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 [] {"", ""}, 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 have 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 ();
}


/** Pop-up box containing input 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 are clicking the OK button! ");
}
});
Builder. setNegativeButton ("cancel", new DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
SetTitle ("You are clicking 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 the 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 ();
}
/** Close the download box after the download */
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;
}
}
}

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.