Android customizes the dialog class and implements button snooping in activity.

Source: Internet
Author: User

In the actual development, often use dialog, such as exit when will pop out or not, or some edit box will also be implemented with dialog, as follows:

The problem with development is that if you listen to the buttons implemented in this dialog in activity, the dialog class implements a Leavemydialoglistener interface in the Mydialog class to implement the onclick click event:

Package com. Ieasy.tool;import com. Ieasy.ieasyware.r;import Android.app.dialog;import Android.content.context;import Android.os.Bundle;import Android.view.view;import Android.widget.button;import Android.widget.textview;public class MyDialog extends Dialog    Implements Android.view.View.OnClickListener {Private context context;    Private TextView txt;    Private Button Btnok,btnedit,btncancle,btnsave;        Private Leavemydialoglistener listener;       public interface leavemydialoglistener{public void OnClick (view view);        } public Mydialog (context context) {super (context);    TODO auto-generated constructor stub this.context = context;        } public Mydialog (Context context,int Theme,leavemydialoglistener listener) {super (context,theme);        TODO auto-generated constructor stub this.context = context;    This.listener = listener;    } @Override protected void OnCreate (Bundle savedinstancestate) {    TODO auto-generated Method Stub super.oncreate (savedinstancestate); This.setcontentview (COM.        Ieasy.ieasyware.r.layout.mydialog);        Btncancle = (Button) Findviewbyid (r.id.mycancle);        Btnedit = (Button) Findviewbyid (R.id.myedit);        Btnok = (Button) Findviewbyid (R.id.myok);        txt = (TextView) Findviewbyid (R.id.miaosu);        Btnsave = (Button) Findviewbyid (R.id.mysave);        Btncancle.setonclicklistener (this);        Btnedit.setonclicklistener (this);        Btnok.setonclicklistener (this);    Btnsave.setonclicklistener (this);    } @Override public void OnClick (View v) {//TODO auto-generated Method stub Listener.onclick (v); }}

The layout file is as follows:

<?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=" match_parent "android:orientation=" vertical " > <linearlayout android:layout_width= "250dp" android:layout_height= "Wrap_content" Android:la Yout_margin= "20DP" android:background= "@drawable/night_biz_subscribe_media_recommend_item_bg" android:orient ation= "vertical" > <textview android:id= "@+id/miaosu" android:layout_width= "Wrap_conten            T "android:layout_height=" wrap_content "android:paddingleft=" 20DP "android:text=" description " Android:textcolor= "@color/whitesmoke" android:textsize= "20sp"/> <linearlayout A Ndroid:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:layout_margintop= "50d P "android:orientation="Horizontal "> <button android:id=" @+id/myok "android:layout_width=" Match_pa Rent "android:layout_height=" match_parent "android:layout_weight=" 1 "android: background= "@drawable/btnclick" android:textcolor= "@color/whitesmoke" android:text= "OK"/&gt            ; <button android:id= "@+id/myedit" android:layout_width= "Match_parent" Andro id:layout_height= "Match_parent" android:layout_weight= "1" android:background= "@drawable/BTNC Lick "android:textcolor=" @color/whitesmoke "android:text=" edit "/> & Lt Button android:id= "@+id/mysave" android:layout_width= "Match_parent" android:l ayout_height= "Match_parent" android:layout_weight= "1" android:background= "@drawable/btnclick       "         Android:textcolor= "@color/whitesmoke" android:text= "Save"/> <button Android:id= "@+id/mycancle" android:layout_width= "Match_parent" android:layout_height= "match _parent "android:layout_weight=" 1 "android:background=" @drawable/btnclick "an Droid:textcolor= "@color/whitesmoke" android:text= "Cancel"/> </LinearLayout> &lt ;/linearlayout></linearlayout>

Referenced style:

<style name= "Mydialog" parent= "@android: Theme.dialog" >        <item name= "Android:windowframe" > @null < /item>        <item name= "Android:windownotitle" >true</item>         <item name= "Android: Windowbackground "> @drawable/night_biz_subscribe_media_recommend_item_bg</item>        <item name=" Android:windowisfloating ">true</item>        <item name=" Android:windowcontentoverlay "> @null </ Item>    </style>

Finally called in the activity, through the Leavemydialoglistener interface to achieve the click event in the activity


Mydialog dialog = new Mydialog (Context,r.style.mydialog, new Mydialog.leavemydialoglistener () { @Override Public void OnClick (view view) { switch (View.getid ()) {case r.id.myok: Break ; Case R.id.myedit:
break; Case r.id.mycancle: Break ; Case R.id.mysave: Dialog.dismiss (); Default: Break;}} ); Dialog.show ();

If you want to get dialog in the TextView control can be obtained, assign value to TextView must be in the dialog show after the assignment, you understand.

TextView Text = (TextView) Dialog.findviewbyid (R.id.miaosu);

Android customizes the dialog class and implements button snooping in activity.

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: 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.