The example in this article is about Alertdialog, which is frequently encountered in this dialog box. Alertdialog is not the same as the dialog in WIN32 development, Alertdialog is non-blocking, and the Blocked dialog box is Popupwindow.
First, post a screenshot of the program's operation:
Main.xml's source code:
<?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"
>
<button android:id= "@+id/button01" android:layout_height= "Wrap_content" android:text= "Layout dialog android:layout_width=" fill_parent "></Button>
<button android:id=" @+id /button02 "android:layout_height=" wrap_content "android:text= Layout Dialog" android:layout_width= "Fill_parent" > </button><view android:id= "@+id/view01" android:layout_width= wrap_content "android:layout_height=" Wrap_ Content "></View>
</LinearLayout>
The following figure is a non-layout dialog box, using Alertdialog directly
The following figure uses the Layout dialog box to customize the controls to implement more complex dialog boxes
Dialoglayout.xml's source code:
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:layout_width=" fill_parent "android:layout_height=" wrap_content "
android:o" rientation= "vertical" >
<edittext android:layout_height= "wrap_content" android:layout_width= "Fill_"
Parent "android:layout_marginleft=" 20dip "
android:layout_marginright=" 20dip "android:textappearance="? Android:attr/textappearancemedium "android:id=" @+id/edtinput "/>
</LinearLayout>
Program Source:
Package com.testalertdialog;
Import android.app.Activity;
Import Android.app.AlertDialog;
Import Android.content.Context;
Import Android.content.DialogInterface;
Import Android.os.Bundle;
Import android.view.Gravity;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.EditText;
Import Android.widget.PopupWindow;
public class Testalertdialog extends activity {Button btnshowdialog;
Button btnshowdialog_layout; /** called the activity is a.
* * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Define button btnshowdialog= (buttons) This.findviewbyid (R.id.button01);
Btnshowdialog.setonclicklistener (New Clickevent ());
btnshowdialog_layout= (Button) This.findviewbyid (R.ID.BUTTON02);
Btnshowdialog_layout.setonclicklistener (New Clickevent ()); }//Unified handling key Event ClasS clickevent implements onclicklistener{@Override public void OnClick (View v) {//TODO auto-generated method s
Tub if (v==btnshowdialog) ShowDialog (testalertdialog.this);
else if (v==btnshowdialog_layout) showdialog_layout (testalertdialog.this); }//Show basic alertdialog private void ShowDialog (context context) {Alertdialog.builder Builder = new Alertdialog.bu
Ilder (context);
Builder.seticon (R.drawable.icon);
Builder.settitle ("Title");
Builder.setmessage ("message"); Builder.setpositivebutton ("Button1", new Dialoginterface.onclicklistener () {public void OnClick (Dialoginterface dial
OG, int whichbutton) {settitle ("Clicked Button1 on Dialog box");
}
}); Builder.setneutralbutton ("Button2", new Dialoginterface.onclicklistener () {public void OnClick (Dialoginterface dialo
g, int whichbutton) {settitle ("Clicked Button2 on Dialog box");
}
}); Builder.setnegativebutton ("Button3", new Dialoginterface.onclicklistener () {public void OnClick (DialogInterface dialog, int whichbutton) {settitle ("Clicked Button3 on Dialog box");
}
});
Builder.show (); }//Display alertdialog private void showdialog_layout based on Layout (context context) {Layoutinflater Inflater = layoutinflate
R.from (this);
Final View Textentryview = inflater.inflate (r.layout.dialoglayout, NULL);
Final EditText edtinput= (edittext) Textentryview.findviewbyid (r.id.edtinput);
Final Alertdialog.builder Builder = new Alertdialog.builder (context);
Builder.setcancelable (FALSE);
Builder.seticon (R.drawable.icon);
Builder.settitle ("Title");
Builder.setview (Textentryview); Builder.setpositivebutton ("Confirm", new Dialoginterface.onclicklistener () {public void OnClick (Dialoginterface dialog, I
NT Whichbutton) {Settitle (Edtinput.gettext ());
}
}); Builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {public void OnClick (Dialoginterface dialog, I
NT Whichbutton) {settitle ("");
}
});
Builder.show (); }
}