用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - android代码库

自定义Dialog

2015-02-25 作者: 云代码会员举报

[android]代码库

package com.yx.straightline.widget;

import com.yx.straightline.R;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

public class AlertDialog extends Dialog implements OnClickListener {

	private TextView tv_title, tv_message;
	private IBaseDialogListener dialogListener;

	private View titleLayout;

	private String title, message, left, right;

	public AlertDialog(Context context) {
		super(context);
		this.dialogListener = (IBaseDialogListener) context;
	}

	public AlertDialog(Context context, String message) {
		super(context);
		this.message = message;
		this.dialogListener = (IBaseDialogListener) context;
	}

	public AlertDialog(Context context, String title,
			String positiveButtonText, String negativeButtonText) {
		super(context);
		this.title = title;
		this.left = positiveButtonText;
		this.right = negativeButtonText;
		this.dialogListener = (IBaseDialogListener) context;
	}

	public AlertDialog(Context context, String title, String message,
			String positiveButtonText, String negativeButtonText) {
		super(context);
		this.title = title;
		this.message = message;
		this.left = positiveButtonText;
		this.right = negativeButtonText;
		this.dialogListener = (IBaseDialogListener) context;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.dialog_layout);
		tv_title = (TextView) findViewById(R.id.tv_dtitle);
		tv_message = (TextView) findViewById(R.id.tv_message);
		titleLayout = findViewById(R.id.title_layout);

		findViewById(R.id.ll_dlayout).setVisibility(View.VISIBLE);
		Button btn_left = (Button) findViewById(R.id.btn_dleft);
		Button btn_right = (Button) findViewById(R.id.btn_dright);

		btn_left.setText(left);

		btn_right.setText(right);

		btn_left.setOnClickListener(this);
		btn_right.setOnClickListener(this);

		titleLayout.setVisibility(View.VISIBLE);
		tv_title.setText(title);

		if(message != null&&!message.equals(""))
		{	tv_message.setVisibility(View.VISIBLE);
			tv_message.setText(message);
		}

		windowDeploy();
	}

	public interface IBaseDialogListener {

		public void onPositiveButtonClicked(View v);

		public void onNegativeButtonClicked(View v);

	}

	@SuppressWarnings("deprecation")
	public void windowDeploy() {
		Window window = getWindow(); // 得到对话框
		window.setBackgroundDrawableResource(R.color.transparent);
		WindowManager.LayoutParams wl = window.getAttributes();
		wl.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
		wl.gravity = Gravity.CENTER;
		WindowManager m = window.getWindowManager();
		Display d = m.getDefaultDisplay(); // 为获取屏幕宽、高
		wl.width = d.getWidth();
		wl.height = d.getHeight();
		window.setAttributes(wl);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_dleft:// 确定按钮点击
			dialogListener.onPositiveButtonClicked(v);
			break;
		case R.id.btn_dright:// 取消按钮点击
			dialogListener.onNegativeButtonClicked(v);
			break;
		default:
			break;
		}
		dismiss();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			return true;
		}
		return false;
	}

	@Override
	public void dismiss() {
		super.dismiss();
		title = null;
		message = null;
		left = null;
		right = null;
	}

	public AlertDialog setTitle(String title) {
		this.title = title;
		return this;
	}

	public AlertDialog setMessage(String message) {
		this.message = message;
		return this;
	}

	public AlertDialog setPositiveButtonText(String text) {
		this.left = text;
		return this;
	}

	public AlertDialog setNegativeButtonText(String text) {
		this.right = text;
		return this;
	}

	public void show(IBaseDialogListener dialogListener) {
		this.dialogListener = dialogListener;
		show();
	}
	
	public void registerListener(IBaseDialogListener listener)
	{
		dialogListener = listener;
	
	};
}

说明:可以在Activity中实现接口,也可以在注册接口
知识点:通知;A通知B,B中有A,给A赋值


网友评论    (发表评论)

共2 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...