Android自己定義提示框

weixin_33896726發表於2017-06-02

    在開發中,假設感覺系統自帶的提示框不好看,開發人員能夠自定義提示框的樣式。主要是繼承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);
	}
}
在呼叫時也很easy

@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

相關文章