android dialog的封装,android Dialog 自定义 封装

本文代码是将一个title和两个button的自定义dialog封装了一下,用的时候只需设置contentview即可,方便于较多使用相同dialog样式但内容不同的情况。

CommonDialog.java

package com.wzf.common;

import android.app.Activity;

import android.app.Dialog;

import android.view.Display;

import android.view.LayoutInflater;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.Window;

import android.view.WindowManager.LayoutParams;

import android.widget.Button;

import android.widget.FrameLayout;

import android.widget.TextView;

import com.wzf.R;

/**

* useage

*

 
 

* DialogClickListener mDialogClickListener = new DialogClickListener() {

* @Override

* public void onOkClickListener(Dialog dialog) {

* Toast.makeText(ManageRoomListActivity.this, "ok", Toast.LENGTH_SHORT).show();

* dialog.dismiss();

* }

* @Override

* public void onCancelClickListener(Dialog dialog) {

* Toast.makeText(ManageRoomListActivity.this, "cancel", Toast.LENGTH_SHORT).show();

* dialog.dismiss();

* }

* };

*

* private void showDialog() {

* CommonDialog commonDialog = new CommonDialog(this);

* final Dialog dialog = commonDialog.setTitle(R.string.dialog_title_add_room)

* .setContentView(R.layout.base_dialog_content_add_room)

* .setOkClickListener(mDialogClickListener)

* .setCancelListener(mDialogClickListener)

* .create();

* FrameLayout parent = commonDialog.getContentParent();

*

* ImageView image = (ImageView) parent.findViewById(R.id.image);

* EditText edit = (EditText) parent.findViewById(R.id.edit);

* }

*

* */

public class CommonDialog {

Activity mActivity;

TextView mTitle;

Button mOk;

Button mCancel;

FrameLayout mFrameLayout;

Dialog mDialog;

DialogClickListener mOkClickListener;

DialogClickListener mCancelClickListener;

int mTitleResId = INVALID_RES_ID;

int mContentResId = INVALID_RES_ID;

static final int INVALID_RES_ID = 0;

public CommonDialog(Activity a) {

mActivity = a;

mDialog = new Dialog(mActivity, R.style.base_dialog);

mDialog.show();

Window window = mDialog.getWindow();

Display d = mActivity.getWindowManager().getDefaultDisplay();

LayoutParams p = window.getAttributes();

p.width = (int) (d.getWidth() * 0.95);

window.setAttributes(p);

window.setContentView(R.layout.base_dialog_layout);

mTitle = (TextView) window.findViewById(R.id.title);

mFrameLayout = (FrameLayout) window.findViewById(R.id.frame_layout);

mCancel = (Button) window.findViewById(R.id.btn_cancel);

mOk = (Button) window.findViewById(R.id.btn_ok);

}

public Dialog create() {

if (mTitleResId != INVALID_RES_ID) mTitle.setText(mTitleResId);

if (mContentResId != INVALID_RES_ID) LayoutInflater.from(mActivity).inflate(mContentResId, mFrameLayout);

if (mOkClickListener != null) mOk.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

mOkClickListener.onOkClickListener(mDialog);

}

});

if (mCancelClickListener != null) mCancel.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

mCancelClickListener.onCancelClickListener(mDialog);

}

});

return mDialog;

}

public FrameLayout getContentParent() {

return mFrameLayout;

}

public CommonDialog setTitle(int resId) {

mTitleResId = resId;

return this;

}

public CommonDialog setOkClickListener(DialogClickListener l) {

mOkClickListener = l;

return this;

}

public CommonDialog setCancelListener(DialogClickListener l) {

mCancelClickListener = l;

return this;

}

public CommonDialog setContentView(int resId) {

mContentResId = resId;

return this;

}

public interface DialogClickListener {

void onOkClickListener(Dialog dialog);

void onCancelClickListener(Dialog dialog);

}

}

styles.xml

@null

true

false

true

@null

false

base_dialog_layout.xml

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:background="@color/dialog_bg" >

android:id="@+id/title"

android:layout_width="match_parent"

android:layout_height="@dimen/dialog_title_height"

android:paddingLeft="20dp"

android:gravity="center_vertical"

android:textSize="@dimen/dialog_title_size"

android:textColor="@color/white"

android:background="@color/dialog_bg_title"/>

android:id="@+id/frame_layout"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:paddingTop="10dp"

android:paddingBottom="10dp"

android:paddingLeft="20dp"

android:paddingRight="20dp"/>

"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:paddingBottom="10dp"

android:gravity="center_horizontal" >

android:id="@+id/btn_cancel"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="wrap_content"

android:layout_marginLeft="@dimen/dialog_btn_margin"

android:background="@drawable/button_selector"

android:text="@string/cancel"

android:textColor="@color/black"

android:textSize="@dimen/dialog_middle_text_size" />

android:id="@+id/btn_ok"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="wrap_content"

android:layout_marginLeft="@dimen/dialog_btn_margin"

android:layout_marginRight="@dimen/dialog_btn_margin"

android:background="@drawable/button_selector"

android:text="@string/ok"

android:textColor="@color/dialog_bg_btn_ok"

android:textSize="@dimen/dialog_middle_text_size" />

布局中使用的一些res id 这里就不列出了,调一下就行了。

使用方法在java文件注释里。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值