AlertDialog dialog box in android
Created by AlertDialog. builder and displayed in show after creation. The Code is as follows:
Xml definition of the dialog box
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "vertical">
<TextView
Android: id = "@ + id/username"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_marginLeft = "20dip"
Android: layout_marginRight = "20dip"
Android: text = "Account"
Android: gravity = "left"
Android: textAppearance = "? Android: attr/textAppearanceMedium"
/>
<EditText
Android: id = "@ + id/username"
Android: layout_height = "wrap_content"
Android: layout_width = "fill_parent"
Android: layout_marginLeft = "20dip"
Android: layout_marginRight = "20dip"
Android: scrollHorizontally = "true"
Android: autoText = "false"
Android: capitalize = "none"
Android: gravity = "fill_horizontal"
Android: textAppearance = "? Android: attr/textAppearanceMedium"
/>
<TextView
Android: id = "@ + id/password"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_marginLeft = "20dip"
Android: layout_marginRight = "20dip"
Android: text = "password"
Android: gravity = "left"
Android: textAppearance = "? Android: attr/textAppearanceMedium"
/>
<EditText
Android: id = "@ + id/password"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_marginLeft = "20dip"
Android: layout_marginRight = "20dip"
Android: scrollHorizontally = "true"
Android: autoText = "false"
Android: capitalize = "none"
Android: gravity = "fill_horizontal"
Android: password = "true"
/>
</LinearLayout>
Java code
Package zziss. android. dialogtest;
Import android. app. Activity;
Import android. app. AlertDialog;
Import android. app. AlertDialog. Builder;
Import android. app. Dialog;
Import android. app. ProgressDialog;
Import android. content. Context;
Import android. content. DialogInterface;
Import android. OS. Bundle;
Import android. view. LayoutInflater;
Import android. view. View;
Import android. widget. Toast;
Public class DialogTestActivity extends Activity {
/** Called when the activity is first created .*/
ProgressDialog m_dialog;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
// Builder builder = new AlertDialog. Builder (DialogTestActivity. this );
// Dialog dialog = builder. create ();
// Dialog. setTitle ("Logon prompt ");
/* Dialog dialog = new AlertDialog. Builder (DialogTestActivity. this)
. SetTitle ("Logon prompt ")
. SetMessage ("login required here ")
. SetPositiveButton ("OK ",
New DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
// TODO Auto-generated method stub
LayoutInflater factory = LayoutInflater. from (DialogTestActivity. this );
Final View DialogView = factory. inflate (R. layout. dialog, null );
AlertDialog dlg = new AlertDialog. Builder (DialogTestActivity. this)
. SetTitle ("logon box ")
. SetView (DialogView)
. SetPositiveButton ("OK ",
New DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
// TODO Auto-generated method stub
M_dialog = ProgressDialog. show (
DialogTestActivity. this,
"Please wait...", "logging on... for you...", true );
New Thread ()
{
Public void run ()
{
Try
{
Sleep (3000 );
}
Catch (Exception e)
{
E. printStackTrace ();
}
Finally
{
M_dialog.dismiss ();
}
}
}. Start (); // end thread
} // End OK click
}
)
. SetNegativeButton ("cancel ",
New DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
// TODO Auto-generated method stub
DialogTestActivity. this. finish ();
}
}
). Create ();
Dlg. show ();
};
}
)
. SetNeutralButton ("exit ",
New DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
// TODO Auto-generated method stub
DialogTestActivity. this. finish ();
}
}
). Create ();
Dialog. show ();*/
InitDialogClick ();
AlertDialog. Builder builder = new AlertDialog. Builder (DialogTestActivity. this );
Builder. setTitle ("Logon dialog box ");
Builder. setMessage ("login required here ")
. SetPositiveButton ("OK", dialogclick)
. SetNegativeButton ("cancel", dialogclick );
Dialog dialog = builder. create ();
Dialog. show ();
}
Private DialogInterface. OnClickListener dialogclick;
Private DialogInterface. OnClickListener dialogLoginClick;
Private void initDialogClick ()
{
Dialogclick = new DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
// TODO Auto-generated method stub
If (which = dialog. BUTTON_POSITIVE)
{
ShowLoginDialog ();
}
If (which = dialog. BUTTON_NEGATIVE)
{
ShowMessage ("click Cancel button ");
}
}
};
DialogLoginClick = new DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
// TODO Auto-generated method stub
If (which = dialog. BUTTON_POSITIVE)
{
ShowProgressDialog ();
}
If (which = dialog. BUTTON_NEGATIVE)
{
Dialog. dismiss ();
}
}
};
} // End initDialogClick
Private void showLoginDialog ()
{
LayoutInflater inflater = (LayoutInflater) this. getSystemService (Context. LAYOUT_INFLATER_SERVICE );
View logindialog = inflater. inflate (R. layout. dialog, null );
AlertDialog login = new AlertDialog. Builder (this)
. SetTitle ("Log on ")
. SetView (logindialog)
. SetPositiveButton ("login", dialogLoginClick)
. SetNegativeButton ("cancel", dialogLoginClick)
. Create ();
Login. show ();
}
Private void showProgressDialog ()
{
M_dialog = ProgressDialog. show (
DialogTestActivity. this,
"Please wait...", "logging on... for you...", true );
New Thread ()
{
Public void run ()
{
Try
{
Sleep (3000 );
}
Catch (Exception e)
{
E. printStackTrace ();
}
Finally
{
M_dialog.dismiss ();
}
}
}. Start (); // end thread
}
Private void showMessage (String str)
{
Toast toast = Toast. makeText (this, str, Toast. LENGTH_SHORT );
Toast. show ();
}
}
The code in the above comments is written in the book, which is not easy to understand, so it is split.
Click a screen other than alertdialog, And the alertdialog disappears/dismiss
It's easy to set the alertDialog attributes:
AlertDialog alert = builder.create();
Alert. setCanceledOnTouchOutside (true); // For details, refer to the official documentation.
alert.show();