Android self-defined cue box

Source: Internet
Author: User

In development, the developer can customize the style of the cue box by assuming that the system comes with a bad cue box. is mainly inherited dialog


Program folder structure


Key code

Package Com.dzt.custom.dialog;import Android.app.dialog;import Android.content.context;import Android.content.res.resources;import Android.util.displaymetrics;import Android.view.gravity;import Android.view.view;import Android.view.window;import Android.view.windowmanager;import android.widget.TextView;/**  * Define your own dialog box * * */public class Customdialog extends Dialog implementsandroid.view.View.OnClickListener {private static int Default_width = 400; Default width private static int default_height = 200;//default height private static String Mshowtext = null;private TextView MText = null ;p ublic Customdialog (context context) {super ';} Public Customdialog (context context, int layout, int style, String msg) {This (context, Default_width, Default_height, Layo UT, style, msg);} Public Customdialog (context context, int width, int height, int layout,int style, String msg) {Super (context, style);//settings Content Setcontentview (layout); mshowtext = Msg;initwidgets ();//Set form Properties Window window = GetWindow (); Windowmanager.layoUtparams params = window.getattributes ();//set width, height, density, alignment float density = getdensity (context);p arams.width = (int) ( Width * density);p arams.height = (int) (height * density);p arams.gravity = gravity.center;window.setattributes (params);} @Overrideprotected void OnStop () {//TODO auto-generated method Stubsuper.onstop (); System.out.println ("Stop");} /** * Initialize the control in the prompt box */private void Initwidgets () {Customimagebutton btn = (Customimagebutton) Findviewbyid (R.ID.BTN_OK); btn . Setonclicklistener (this), BTN = (Customimagebutton) Findviewbyid (r.id.btn_cancel); Btn.setonclicklistener (this); MText = (TextView) Findviewbyid (R.id.dlg_tv_text); Mtext.settext (Mshowtext);} /** * Get display density * * @param context * @return */public float getdensity (context context) {Resources res = Context.getresource S ();D isplaymetrics dm = Res.getdisplaymetrics (); return dm.density;} @Overridepublic void OnClick (View v) {//TODO auto-generated method Stubswitch (V.getid ()) {Case R.id.btn_ok:if (listener ! = null) Listener.onclickok (); SysteM.OUT.PRINTLN ("You click OK button---------"); Break;case r.id.btn_cancel:if (listener! = null) Listener.onclickcancel ( ); SYSTEM.OUT.PRINTLN ("You click cancel button-------"); break;default:break;}} public void Setonclickbtnlistener (Onclickbtnlistener listener) {This.listener = listener;} Private Onclickbtnlistener listener = Null;public interface Onclickbtnlistener {public void Onclickok ();p ublic void oncli Ckcancel ();}}
The listener is set up in the Prompt box for two buttons. Buttons are also defined by themselves

Package Com.dzt.custom.dialog;import Android.content.context;import Android.content.res.typedarray;import Android.graphics.canvas;import Android.graphics.paint;import Android.graphics.paint.align;import Android.util.attributeset;import Android.util.log;import android.widget.imagebutton;/** * Own definition ImageButton Ability to set text on ImageButton */public class Customimagebutton extends ImageButton {private static final String TAG = "Customimageb Utton_dzt ";p rivate String mtext =" ";p rivate int mcolor = 0;private float mtextsize = 0f;private Paint mpatin;public custo Mimagebutton (context context, AttributeSet Attrs) {Super (context, attrs); Initattrs (attrs);} private void Initattrs (AttributeSet attrs) {TypedArray array = GetContext (). Obtainstyledattributes (Attrs, R.STYLEABLE.CUSTOMBUTTONATTRS); mtext = array.getstring (r.styleable.custombuttonattrs_textvalue); Mcolor = Array.getcolor (r.styleable.custombuttonattrs_textcolor), mtextsize = Array.getdimension ( r.styleable.custombuttonattrs_textsize,25.0f); array.reCycle (); Recycling Resources Mpatin = new Paint (); mpatin.settextalign (Align.center); LOG.D (TAG, "mtextsize =" + mtextsize);} public void SetText (String text) {this.mtext = text;} public void SetColor (int color) {this.mcolor = color;} public void Settextsize (float textsize) {this.mtextsize = Textsize;} @Overrideprotected void OnDraw (canvas canvas) {super.ondraw (canvas); Mpatin.setcolor (Mcolor); Mpatin.settextsize ( mtextsize); Canvas.drawtext (Mtext, Canvas.getwidth ()/2, (Canvas.getheight ()/2) +10, Mpatin);}}
It's easy to call, too.

@Overridepublic void OnClick (View v) {//TODO auto-generated method Stubswitch (V.getid ()) {case R.id.btn_show_dialog:cus Tomdialog = new Customdialog (this, R.layout.dialog_layout,r.style.dialogtheme, Getresources (). GetString ( r.string.text_show)); Customdialog.show (); Customdialog.setonclickbtnlistener (new Onclickbtnlistener () {@ overridepublic void Onclickok () {//TODO auto-generated method StubSystem.out.println ("You click OK button");//Handle Click OK To make//... customdialog.cancel (); "." (); @Overridepublic void Onclickcancel () {//TODO auto-generated method StubSystem.out.println ("You click cancel Button");// Handle the action when you click Cancel//..... customdialog.cancel ();....;}); Break;default:break;}}
Need source code to: http://download.csdn.net/detail/deng0zhaotai/7760615

Android self-defined cue box

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.