目的:为了app中有统一的弹窗显示
1.代码
public class CustomDialog extends AlertDialog{
private Button mBtnOk;
private Button mBtnCancel;
private TextView tvTitle;
private TextView tvContent;
protected CustomDialog(Context context) {
super(context);
}
protected CustomDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
}
protected CustomDialog(Context context, int themeResId) {
super(context, themeResId);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(getWindow()!=null){
getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
}
setContentView(R.layout.customdialog);
initView();
}
private void initView() {
mBtnOk = (Button) findViewById(R.id.ok);
mBtnCancel = (Button) findViewById(R.id.cancel);
tvTitle = (TextView) findViewById(R.id.dialog_title);
tvContent = (TextView) findViewById(R.id.dialog_content);
}
public void setTexts(CharSequence title,CharSequence content,CharSequence ok,CharSequence cancle){
//标题和内容有一个为空,则在内容区显示
if(TextUtils.isEmpty(title)||TextUtils.isEmpty(content)){
tvTitle.setVisibility(View.GONE);
tvContent.setText(TextUtils.isEmpty(title)?content:title);
}else{
tvTitle.setText(title);
tvContent.setText(content);
}
if(TextUtils.isEmpty(ok)||TextUtils.isEmpty(cancle)){
mBtnCancel.setVisibility(View.GONE);
mBtnOk.setText(TextUtils.isEmpty(ok)?cancle:ok);
}else{
mBtnOk.setText(ok);
mBtnCancel.setText(cancle);
}
}
public void setListeners(final OnClickListener positiveListener,
final OnClickListener negativeListener){
mBtnOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(positiveListener!=null) {
dismiss();
positiveListener.onClick(CustomDialog.this, 1);
}else{
dismiss();
}
}
});
mBtnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (negativeListener != null) {
negativeListener.onClick(CustomDialog.this,2);
dismiss();
}else{
dismiss();
}
}
});
}
public void setCanOutSide(boolean isCanTouchOutSize){
setCanceledOnTouchOutside(isCanTouchOutSize);
}
public void setKeyBack(final boolean flag){
setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
return flag;
}
});
}
}
2.布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent">
<LinearLayout
android:layout_width="270dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/update_backgroud"
android:gravity="center_horizontal"
android:minHeight="180dp"
android:orientation="vertical"
android:visibility="visible">
<!--标题-->
<TextView
android:id="@+id/dialog_title"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:visibility="visible"
android:textSize="17sp"
android:textColor="@android:color/holo_orange_light" />
<!--内容-->
<TextView
android:id="@+id/dialog_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:padding="22dp"
android:textColor="@android:color/black"
android:textSize="15sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:orientation="horizontal"
android:showDividers="middle"
android:divider="@drawable/divier">
<Button
android:id="@+id/cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:textSize="17sp"
android:visibility="visible"
android:textColor="#dedede" />
<Button
android:id="@+id/ok"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:textSize="17sp"
android:textColor="@android:color/holo_orange_dark" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
3.封装使用
public class TipsUtils {
/**
*
* @param activity
* @param title 标题
* @param content 内容
* @param positiveListener 确定按钮点击监听
* @param negativeListener 取消按钮点击监听
* @param positiveText 确定按钮
* @param negativeText 取消按钮
* @param touchOutside 点击弹窗外部弹窗是否消失
*/
public static void showDialog(Activity activity, CharSequence title
, CharSequence content, OnClickListener positiveListener,
OnClickListener negativeListener,
CharSequence positiveText, CharSequence negativeText,
boolean touchOutside) {
if (activity != null && !activity.isFinishing()) {
CustomDialog dialog = new CustomDialog(activity, touchOutside, null);
dialog.show();
dialog.setTexts(title, content, positiveText, negativeText);
dialog.setListeners(positiveListener, negativeListener);
dialog.setCanOutSide(touchOutside);
if(!touchOutside) {
dialog.setCancelable(touchOutside);
}
}
}
/**
* 只展示内容,没有标题,只有一个确定按钮
* 点击外部弹窗可消失
* @param content
*/
public static void showDialog(Activity activity,CharSequence content){
showDialog(activity,"",content,null,null,"确定","",true);
}
/**
* 只展示内容和标题,只有一个确定按钮
* @param activity
* @param titile
* @param content
*/
public static void showDialog(Activity activity,CharSequence titile,CharSequence content){
showDialog(activity,titile,content,null,null,"确定","",true);
}
/**
*
* @param activity
* @param content
* @param close 关闭当前activity
*/
public static void showDialog(final Activity activity, CharSequence content, final boolean close){
showDialog(activity, "", content, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(close) {
activity.finish();
}
}
}, null, "确定", "", true);
}
TipsUtils.showDialog(this,"内容");
TipsUtils.showDialog(this,"标题","内容");