Android custom prompt box, android custom

Source: Internet
Author: User

Android custom prompt box, android custom

During development, if you feel that the prompt box provided by the system is not easy to understand, developers can customize the style of the prompt box by themselves, mainly inheriting the Dialog


Program directory 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;/*** custom Dialog box ***/public class CustomDialog extends Dialog implementsandroid. view. view. onClickLi Stener {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; public CustomDialog (Context context) {super (context);} public CustomDialog (Context context, int layout, int style, String msg) {this (context, default_width, default_height, layout, style, msg);} public CustomDialog (Context context, Int width, int height, int layout, int style, String msg) {super (context, style); // setContentView (layout); mShowText = msg; initWidgets (); // set Window Properties window Window = getWindow (); WindowManager. layoutParams params = window. getAttributes (); // set the width, height, density, and alignment float density = getDensity (context); params. width = (int) (width * density); params. height = (int) (height * density); params. gravity = Gravity. CENT ER; window. setAttributes (params) ;}@ Overrideprotected void onStop () {// TODO Auto-generated method stubsuper. onStop (); System. out. println ("stop");}/*** initialize the control */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) findVi EwById (R. id. dlg_ TV _text); mText. setText (mShowText);}/*** get display density ** @ param context * @ return */public float getDensity (Context context) {Resources res = context. getResources (); DisplayMetrics 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 (); public void onClickCancel ();}}
The listener is set for the two buttons in the prompt box, And the buttons are also customized.

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;/*** custom ImageButton can set text on ImageButton */public class CustomImageButton extends ImageButton {private static final String TAG = "mimimagebutton_dzt"; private String mtext = ""; private int mcolor = 0; private float mtextsize = 0f; private Paint mpatin; public CustomImageButton (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, 230); mtextsize = array. getDimension (R. styleable. customButtonAttrs_textSize, 25366f); array. recycle (); // reclaim the resource 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 is also very simple to call

@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubswitch (v. getId () {case R. id. btn_show_dialog: customDialog = 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"); // process the operation when you click OK //............... customDialog. cancel () ;}@ Overridepublic void onClickCancel () {// TODO Auto-generated method stubSystem. out. println ("you click cancel button"); // process the operation when you click Cancel //................... customDialog. cancel () ;}}); break; default: break ;}}
Need source code can be: http://download.csdn.net/detail/deng0zhaotai/7760615


Android custom pop-up box style

Good. Thank you.
 
Android custom pop-up box style

Good. Thank you.
 

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.