In this blog, you can learn the following items:
<1> Use of layoutinflater
<2> dialog, alertdialog, and custom Layout
<3> onclick attribute of a button
Run, the interface is two buttons
Custom Dialog
Custom alertdialog
Complete source code: http://download.csdn.net/source/3512363
1. Activity Code
Package mark. zhang; import android. app. activity; import android. app. alertdialog; import android. app. alertdialog. builder; import android. app. dialog; import android. content. dialoginterface; import android. OS. bundle; import android. util. log; import android. view. layoutinflater; import android. view. view; import android. view. view. onclicklistener; import android. view. viewgroup; import android. widget. button; Public CIA SS customdialogactivity extends activity {builder customalertdialog = NULL; dialog custom= NULL; layoutinflater Inflater = NULL; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); Inflater = (layoutinflater) getsystemservice (layout_inflater_service);}/*** set the listener for the customdialog button ** note: set the onclick attribute in the XML file to be consistent with the name changed, no The ** @ Param v * parameter is required */Public void customdialog (view v) {customdialog = new dialog (this); customdialog. setcontentview (R. layout. about); customdialog. settitle ("Custom dialog"); // if this sentence code is set to false, click "return" to exit the dialog // customdialog. setcancelable (false); // view = Inflater. inflate (R. layout. about, (viewgroup) findviewbyid (R. id. aboutlinearview); // button final button btn_ OK = (button) C in the custom Layout Ustomdialog. findviewbyid (R. id. about_button); btn_ OK .setonclicklistener (New onclicklistener () {// This event is triggered @ overridepublic void onclick (view v) {log. D ("mark", "myself dialog! ") ;}}); // Displayed dialog box customdialog. show ();}/*** set the listener for the customalertdialog button ** Note: Set The onclick attribute in the XML file to be consistent with the name changed, otherwise, ** @ Param v * is returned. this parameter is required */Public void customalertdialog (view v) {customalertdialog = new alertdialog. builder (this); // layout/about. XML custom Layout file view = Inflater. inflate (R. layout. about, (viewgroup) findviewbyid (R. id. aboutlinearview); customalertdialog. settitle ("Custom alertdialog "). se Tview; customalertdialog. setpositivebutton ("OK", new dialoginterface. onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {dialog. cancel () ;}}); // button final button btn_ OK = (button) view in the custom layout. findviewbyid (R. id. about_button); btn_ OK .setonclicklistener (New onclicklistener () {// this event can be triggered @ overridepublic void onclick (view v) {log. D ("mark", "myself alertdialog! "); // Other operations, such as killing the activity, starting the service, and another activity}); // displayed dialog box customalertdialog. Show ();}}
2. 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:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="5dip" android:text="customDialog" android:onClick="customDialog" /><Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dip" android:text="customAlertDialog" android:onClick="customAlertDialog" /></LinearLayout>
Note that the onclick attribute is set for both buttons. Check that there are two corresponding methods in the activity code.
3. custom Layout file about. xml
<?xml version="1.0" encoding="utf-8"?><ScrollView xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent" android:layout_height="fill_parent"android:id="@+id/AboutScrollView"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/AboutLinearView"android:layout_width="fill_parent" android:layout_height="fill_parent"android:orientation="vertical">><TextView android:layout_width="wrap_content"android:layout_height="wrap_content" android:id="@+id/AboutTextView"android:textColor="?android:attr/textColorPrimaryDisableOnly"android:textAppearance="?android:attr/textAppearanceSmall"android:autoLink="all" android:padding="11dp" android:text="@string/about_text" /><Button android:id="@+id/about_button" android:layout_width="wrap_content"android:layout_height="wrap_content" android:layout_gravity="center_horizontal"android:text="ok,I know!" /></LinearLayout></ScrollView>