[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赋值
初级程序员
by: 云代码会员 发表于:2016-10-14 02:39:43 顶(0) | 踩(0) 回复
回复评论