Create a new layout file My_dialog.xml
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "> <textview Android:id= "@+id/textview1" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_alignparentleft= "true" android:layout_alignparenttop= "true" android:layout_margintop= "4DP" android:text= "@string/textview1"/> <edittext android:id= "@+id/edittext_user" an Droid:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:layout_alignleft= "@+id/textVie W1 "android:layout_below=" @+id/textview1 "android:layout_margintop=" 4DP "/> <textview Android:id= "@+id/textview2" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_alignleft= "@+id/Edittext_user "android:layout_below=" @+id/edittext_user "android:layout_margintop=" 7DP "android:text= "@string/textview2"/> <edittext android:id= "@+id/edittext_pwd" android:layout_width= "f Ill_parent "android:layout_height=" wrap_content "android:layout_alignleft=" @+id/textview2 "Android:la yout_margintop= "4DP" android:layout_below= "@+id/textview2"/></relativelayout>
The following is the Strings.xml file:
<?xml version= "1.0" encoding= "Utf-8"?><resources> <string name= "App_name" >demo091801</ string> <string name= "action_settings" >Settings</string> <string name= "Hello_world" >hello world!</string> <string name= mydialog > Customize dialog box </string> <string name= " TextView1 "> User name </string> <string name=" textView2 "> Password </string></resources>
Add a button control to the main activity_main.xml, and when you click this button, the custom dialog box is played:
<relativelayout 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:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin " android:paddingright=" @dimen/activity_horizontal_margin " android:paddingtop=" @dimen /activity_vertical_margin " tools:context=". Mainactivity "> <button android:id=" @+id/my_dialog " android:layout_width=" Fill_parent " android:layout_height= "Wrap_content" android:text= "@string/mydialog" /></relativelayout>
The graphical interface is as follows:
In the Mainactivity.java code, set the Click event Listener for the button control and bounce the custom dialog box:
Mainactivity.java:
Package Com.example.demo091801;import Android.os.bundle;import Android.app.activity;import android.app.AlertDialog ; Import Android.content.dialoginterface;import Android.view.layoutinflater;import Android.view.menu;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;public class MainActivity Extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Button btn1 = (button) Findviewbyid (R.id.my_dialog); Btn1.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View arg0) {//TODO auto-generated method Stubalertdialog.builder Builder = new Alertdialog.builder (mainacti Vity.this); Layoutinflater inflater = Getlayoutinflater (); View view = Inflater.inflate (R.layout.my_dialog, null); Builder.settitle ("Login"); Builder.setview (view); Builder.setpositivebutton ("Login", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (dialoginterface ARG0, int arg1) {//TODO auto-generated Method stub}}); Builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (dialoginterface arg0, int arg1) {//TODO auto-generated Method stub}}); Alertdialog dialog = Builder.create ();d ialog.show ();}});} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}
http://blog.csdn.net/lp310018931
Android Customization dialog box