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