Previously, we used simpleadapter to create a custom dialog box. Android also allows us to display the specified XML file in the dialog box to implement the custom dialog box.
Click the button to bring up the custom logon dialog box.
XML file displayed in the dialog box
<? XML version = "1.0" encoding = "UTF-8"?> <Tablelayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Id = "@ + ID/loginform" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: orientation = "vertical"> <tablerow> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "username:" Android: textsize = "10pt"/> <! -- Enter the text box of the user name --> <edittext Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: hint = "Please enter the Logon account" Android: selectallonfocus = "true"/> </tablerow> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "Password:" Android: textsize = "10pt"/> <! -- Enter the password text box --> <edittext Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: password = "true"/> </tablerow> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "phone number: "Android: textsize =" 10pt "/> <! -- Enter the phone number text box --> <edittext Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: hint = "Please enter your phone number" Android: phonenumber = "true" Android: selectallonfocus = "true"/> </tablerow> <button Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: TEXT = "register"/> </tablelayout>
Use the getlayoutinflate. Inflate method to get the XML file to be displayed, and then use the builder. setview method to load the obtained XML file into the dialog box.
Public class logindialog extends activity {@ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); button bn = (button) findviewbyid (R. id. BN); // defines an alertdialog. builder object final Builder = new alertdialog. builder (this); // bind event listener bn to the button. setonclicklistener (new view. onclicklistener () {@ overridepublic void onclick (View Source) {// set the icon in the dialog box Builder. seticon (R. drawable. tools); // set the title builder of the dialog box. settitle ("Custom normal dialog box"); // load/RES/layout/login. XML interface layout tablelayout loginform = (tablelayout) getlayoutinflater (). inflate (R. layout. login, null); // view object builder displayed in the Setting dialog box. setview (loginform); // set a "OK" button builder for the dialog box. setpositivebutton ("login" // set the listener for the button, new onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {// here you can execute the logon process }}); // In the dialog box, set a "cancel" button builder. setnegativebutton ("cancel", new onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {// cancel the logon and do not do anything .}}); // Create and display the dialog box builder. Create (). Show ();}});}}