Android custom dialog box (DIALOG)

Source: Internet
Author: User

Mainactivity is as follows:

Package CN. testalertdialog; import android. app. activity; import android. app. alertdialog. builder; import android. app. dialog; import android. OS. bundle; import android. view. layoutinflater; import android. view. view; import android. view. view. onclicklistener; import android. widget. button;/*** custom dialog usage */public class mainactivity extends activity {private button; private dialog mdialog; @ overrideprote CTED void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); Init ();} private void Init () {button = (button) findviewbyid (R. id. button); button. setonclicklistener (New buttononclicklistenerimpl ();} private class buttononclicklistenerimpl implements onclicklistener {@ overridepublic void onclick (view) {Switch (view. GETID () {case R. id. button: layou Tinflater Inflater = (layoutinflater) getsystemservice (layout_inflater_service); view dialogview = Inflater. inflate (R. layout. dialogview, null); Builder = new Builder (mainactivity. this); builder. setview (dialogview); mdialog = builder. create (); mdialog. show (); initdialogccomponents (dialogview); break; default: Break ;}}// initialize the private void initdialogccomponents (view dialogview) {button firstb Utton = (button) dialogview. findviewbyid (R. id. dialog_firstbutton); firstbutton. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {system. out. println ("click the first button") ;}}); button secondbutton = (button) dialogview. findviewbyid (R. id. dialog_secondbutton); secondbutton. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {If (mdialog! = NULL & mdialog. isshowing () {mdialog. Dismiss ();}}});}}


Main. XML is as follows:

<RelativeLayout xmlns:android=""    xmlns:tools=""    android:layout_width="fill_parent"    android:layout_height="fill_parent"    >        <Button        android:id="@+id/button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/button_tip"         android:textSize="20sp"        android:layout_centerInParent="true"     /></RelativeLayout>

Dialogview. XML is as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "vertical"> <button Android: id = "@ + ID/dialog_firstbutton" Android: layout_width = "150dip" Android: layout_height = "50dip" Android: text = "first button" Android: layout_gravity = "center_horizontal"/> <button Android: Id = "@ + ID/dialog_secondbutton" Android: layout_width = "150dip" Android: layout_height = "50dip" Android: TEXT = "the first button" Android: layout_gravity = "center_horizontal"/> </linearlayout>


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.