在开发中,如果感觉系统自带的提示框不好看,开发者可以自己定义提示框的样式,主要是继承Dialog
程序目录结构
关键代码
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; /** * 自定义对话框 * * */ public class CustomDialog extends Dialog implements android.view.View.OnClickListener { private static int default_width = 400; // 默认宽度 private static int default_height = 200;// 默认高度 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(); // 设置窗口属性 Window window = getWindow(); WindowManager.LayoutParams params = window.getAttributes(); // 设置宽度、高度、密度、对齐方式 float density = getDensity(context); params.width = (int) (width * density); params.height = (int) (height * density); params.gravity = Gravity.CENTER; window.setAttributes(params); } @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); System.out.println("stop"); } /** * 初始化提示框中的控件 */ 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); } /** * 获取显示密度 * * @param context * @return */ public float getDensity(Context context) { Resources res = context.getResources(); DisplayMetrics dm = res.getDisplayMetrics(); return dm.density; } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (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(); } }在提示框中对两个按钮设置了监听器,按钮也是自定义的
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; /** * 自定义ImageButton 可以在ImageButton上面设置文字 */ public class CustomImageButton extends ImageButton { private static final String TAG = "CustomImageButton_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, 25.0f); array.recycle(); // 回收资源 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; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mpatin.setColor(mcolor); mpatin.setTextSize(mtextsize); canvas.drawText(mtext, canvas.getWidth() / 2, (canvas.getHeight() / 2)+10, mpatin); } }在调用时也非常简单
@Override public void onClick(View v) { // TODO Auto-generated method stub switch (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() { @Override public void onClickOk() { // TODO Auto-generated method stub System.out.println("you click ok button"); // 处理点击OK时的操作 // ............... customDialog.cancel(); } @Override public void onClickCancel() { // TODO Auto-generated method stub System.out.println("you click cancel button"); // 处理点击Cancel时的操作 // ................... customDialog.cancel(); } }); break; default: break; } }需要源码的可以到:http://download.csdn.net/detail/deng0zhaotai/7760615
原文地址:http://blog.csdn.net/deng0zhaotai/article/details/38584433