Android Custom Dialog

Source: Internet
Author: User

Android development process, often encounter a number of demand scenarios-pop a box on the interface, to remind users and let users do some selective operation,

If you exit the pop-up window, let the user choose "Quit" or "Cancel" actions.

The Android System provides dialog classes, as well as subclasses of dialog, common as alertdialog to implement such functions.

In general, the dialog and its subclasses provided by Android can meet most of these needs, however, its shortcomings are reflected in:

1. Android-based dialog and its sub-class style is a single, style and the app itself may not be in harmony with the style;

2. The dialog window is limited in layout and functionality, and sometimes does not necessarily meet the actual business requirements.

This article will build a custom dialog pop-up window based on dialog, taking the most common confirmation box as an example.

This style is relatively simple: there is a bullet box title (prompt), the following is the "Confirm" and "Cancel" button, when the user clicks the "Confirm" button, the box execution

The corresponding confirmation logic, when the "Cancel" button is clicked, the corresponding cancellation logic is executed.

First, customize the frame style:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" wrap_content "android:background=" @drawable/D IALOG_BG "android:orientation=" vertical "> <textview android:id=" @+id/title "Android:layout_widt H= "Wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center" android:padding top= "14DP" android:textcolor= "@color/login_hint" android:textsize= "@dimen/text_size_18"/> <linearl Ayout android:layout_width= "match_parent" android:layout_height= "Wrap_content" android:layout_marginb Ottom= "14DP" android:layout_marginleft= "20DP" android:layout_marginright= "20DP" Android:layout_margin            top= "30DP" > <textview android:id= "@+id/confirm" android:layout_width= "Wrap_content" Android:layout_height= "WraP_content "android:layout_marginright=" 10DP "android:layout_weight=" 1 "android:background            = "@drawable/btn_confirm_selector" android:gravity= "center" android:textcolor= "@color/white" Android:textsize= "@dimen/text_size_16"/> <textview android:id= "@+id/cancel" Android            : layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_marginleft= "10DP" android:layout_weight= "1" android:background= "@drawable/btn_cancel_selector" Android:gravi ty= "center" android:textcolor= "@color/login_hint" android:textsize= "@dimen/text_size_16"/> &L T;/linearlayout></linearlayout>

Then build the confirmation box control Confirmdialog by inheriting the dialog class:

Package Com.corn.widget;import Android.app.dialog;import Android.content.context;import android.os.Bundle;import Android.util.displaymetrics;import Android.view.layoutinflater;import Android.view.view;import Android.view.window;import Android.view.windowmanager;import Android.widget.textview;import Com.corn.R;public    Class Confirmdialog extends Dialog {private context context;    Private String title;    Private String Confirmbuttontext;    Private String Cacelbuttontext;    Private Clicklistenerinterface Clicklistenerinterface;        Public interface Clicklistenerinterface {public void doconfirm ();    public void Docancel (); } public Confirmdialog (context context, string title, String Confirmbuttontext, String cacelbuttontext) {Super (        context, R.style.mydialog);        This.context = context;        This.title = title;        This.confirmbuttontext = Confirmbuttontext;    This.cacelbuttontext = Cacelbuttontext; } @Override protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stub super.oncreate (savedinstancestate);    Init ();        } public void Init () {Layoutinflater Inflater = layoutinflater.from (context);        View view = Inflater.inflate (R.layout.confirm_dialog, NULL);        Setcontentview (view);        TextView Tvtitle = (TextView) View.findviewbyid (r.id.title);        TextView tvconfirm = (TextView) View.findviewbyid (r.id.confirm);        TextView Tvcancel = (TextView) View.findviewbyid (r.id.cancel);        Tvtitle.settext (title);        Tvconfirm.settext (Confirmbuttontext);        Tvcancel.settext (Cacelbuttontext);        Tvconfirm.setonclicklistener (New Clicklistener ());        Tvcancel.setonclicklistener (New Clicklistener ());        Window Dialogwindow = GetWindow ();        Windowmanager.layoutparams LP = Dialogwindow.getattributes (); Displaymetrics d = context.getresources (). Getdisplaymetrics (); Get screen width, height with lp.width = (int) (D.widthpixels * 0.8); The height is set to the screen of the 0.6 dialogwindow.setattributes (LP); } public void Setclicklistener (Clicklistenerinterface clicklistenerinterface) {this.clicklistenerinterface = cl    Icklistenerinterface;            } Private class Clicklistener implements View.onclicklistener {@Override public void OnClick (View v) {            TODO auto-generated method stub int id = V.getid ();                Switch (ID) {case r.id.confirm:clicklistenerinterface.doconfirm ();            Break                Case R.id.cancel:clicklistenerinterface.docancel ();            Break }        }    };}

  

In the above space construction code, because the control's "ACK" and "Cancel" logic is related to the actual scenario, the control is implemented by defining an internal interface.

Where this control needs to be used, call the following form

public static void exit (final context context) {        final confirmdialog confirmdialog = new Confirmdialog (Context, "OK to exit ? "," Exit "," Cancel ");        Confirmdialog.show ();        Confirmdialog.setclicklistener (New Confirmdialog.clicklistenerinterface () {            @Override public            Void Doconfirm () {                //TODO auto-generated method Stub                Confirmdialog.dismiss ();                Touserhome (context);                Appmanager.getappmanager (). AppExit (context);            }            @Override public            void Docancel () {                //TODO auto-generated method Stub                Confirmdialog.dismiss ();            }        });    }

The internal interface of this control is implemented in the call and is assigned to the control itself to implement a function callback based on the external specific business logic when the button is clicked.

Android Custom Dialog

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: info-contact@alibabacloud.com 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.