This is a good example of the use of Alertdialog and layoutinflater in detail:
Main.xml
<?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型对话框" android:layout_width="fill_parent"> </Button> <Button android:id="@+id/Button02" android:layout_height="wrap_content" android:text="Layout型对话框" android:layout_width="fill_parent"> </Button> <View android:id="@+id/View01" android:layout_width="wrap_content" android:layout_height="wrap_content"> </View> </LinearLayout>
Dialoglayout.xml
<?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:orientation="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>
Testalertdialog.java
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 when the activity is first created. */@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 ()); } //UnifiedHandle key event class Clickevent implements onclicklistener{@Override public void OnClick (View v) {if (V==btnshowdialog) {showdial OG (testalertdialog.this); }else if (v==btnshowdialog_layout) {showdialog_layout (testalertdialog.this);}} }//DisplayBasicAlertdialog private void ShowDialog (context context) {Alertdialog.builder Builder = new Alertdialog.builder (context); b Uilder.seticon (R.drawable.icon); Builder.settitle ("Title"); Builder.setmessage ("Message"); Builder.setpositivebutton ("Button1", new Dialoginterface.onclicklistener () {public void OnClick (dialoginterface dialog, int whichbutton) {settitle ("click on Button1 on the dialog box");}); Builder.setneutralbutton ("Button2", new Dialoginterface.onclicklistener () {public void OnClick (dialoginterface dialog, int whichbutton) {settitle ("click on Button2 on the dialog box");}); Builder.setnegativebutton ("Button3", new Dialoginterface.onclicklistener () {public void OnClick (dialoginterface dialog, int whichbutton) {settitle ("click on Button3 on the dialog box");}); Builder.show (); }//Display based onLayoutAlertdialog private void Showdialog_layout (context context) {Layoutinflater Inflater = Layoutinflater.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, int Whichbutton) {settitle (Edtinput.gettext ());}}); Builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {public void OnClick (Dialoginterface dialog, int Whichbutton) {settitle ("");}}); Builder.show (); } }
Summary under:
1. For button, you can learn the above, a unified event to deal with.
2. In the actual development of the species layoutinflater this class is still very useful, its role is similar to Findviewbyid (),
The difference is that Layoutinflater is used to find layouts under the XML layout file and instantiate it! Instead, Findviewbyid () is looking for specific widget controls (such as: Button,textview, etc.) under specific XML.
3. Note the alertdialog here. Usage of Builder