一般來說每個APP都有自己統一的樣式或主題,比如整個應用對話框的主題都是一樣的,而每次彈出對話框顯示都要寫那段無聊的代碼,所以我們有必要自己寫一個自定義的對話框類,根據自己所要設置的內容通過工具類把它調用出來顯示,就可以輕鬆實現對話框功能,代碼複用性也更加好。
首先我們要新建一個類去繼續Dialog
public class MCustomDialog extends Dialog implements OnClickListener{
private Context context;
private TextView dailog_tv;
private Button positive_Btn,negative_Btn;
private MyOnClickListener onClickListener;
/**
* @param onClickListener:一個接口:實現自定義的方法
*/
public void setOnClickListener(MyOnClickListener onClickListener) {
this.onClickListener = onClickListener;
}
/**
* 構造器
* @param context 上下文
* @param theme 主題
*/
public MCustomDialog(Context context,int theme) {
super(context,theme);
this.context = context;
init();
initListener();
}
/**
* 初始化監聽
*/
public void initListener() {
positive_Btn.setOnClickListener(this);
negative_Btn.setOnClickListener(this);
}
/**
* 設置正按鈕內容
* @param content
*/
public void setPosiContent(String content){
if(positive_Btn != null){
positive_Btn.setText(content);
}
}
/**
* 設置負按鈕內容
* @param content
*/
public void setNegaContent(String content){
if(negative_Btn != null){
negative_Btn.setText(content);
}
}
private void init() {
View view = View.inflate(context, R.layout.view_custom_dialog, null);
dailog_tv = (TextView)view.findViewById(R.id.dialog_content_tv);
positive_Btn = (Button)view.findViewById(R.id.dialog_positive_btn);
negative_Btn = (Button)view.findViewById(R.id.dialog_negative_btn);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
DisplayUtils.dip2px(context, 300),DisplayUtils.dip2px(context, 150)
);
this.setContentView(view,params);
}
/***
* 設置所需的內容
* @param content:設置的內容
*/
public void setContent(String content){
if(dailog_tv != null && content != null){
dailog_tv.setText(content);
}
}
/**
* 實現點擊事件
*/
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.dialog_positive_btn:
if(onClickListener!=null){
onClickListener.onPositiveListener();
}
MCustomDialog.this.dismiss();
break;
case R.id.dialog_negative_btn:
if(onClickListener!=null){
onClickListener.onNagetiveListener();
}
MCustomDialog.this.dismiss();
break;
default:
break;
}
}
/**
* 自定義一個接口
* @author Administrator
* 兩個方法:接口回調,調用兩個按鈕的點擊事件實現方法
*/
public interface MyOnClickListener{
void onPositiveListener();
void onNagetiveListener();
}
}
對話框的按鈕點擊通過接口暴露到外面,功能邏輯由用戶自己實現。
佈局文件<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="@drawable/defaulting_dialog_bg"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/dialog_content_tv"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="2"
android:gravity="center"
android:text="是不是真要離開?"
android:textColor="#3F3B3F"
android:textSize="20sp" />
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#e7e7e7" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<Button
android:id="@+id/dialog_negative_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
android:gravity="center"
android:text="取消"
android:textColor="#848484" />
<View
android:layout_width="1px"
android:layout_height="match_parent"
android:background="#e7e7e7" />
<Button
android:id="@+id/dialog_positive_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
android:text="確定"
android:gravity="center"
android:textColor="#D26638" />
</LinearLayout>
</LinearLayout>
建立一個對話框工具類去實現對話框的調用,用到了方法重載
public class DialogUtils {
private static MCustomDialog dialog;
/**
* @param context
* @param content:對話框顯示的文本
* @param onMClickListner:對話框回調接口
* 顯示默認對話框
*/
//com.example.radiogroup.R.style.myDialogTheme
public static void showDialog(Context context,String content,MyOnClickListener onClickListener){
showDialog(context, content, onClickListener, null, null);
}
/**
* 顯示對話框
* @param context
* @param content
* @param onClickListener
* @param positiveContent
* @param negativiContent
*/
public static void showDialog(Context context,String content,MyOnClickListener onClickListener,String positiveContent,String negativiContent){
dialog = new MCustomDialog(context, com.example.radiogroup.R.style.myDialogTheme);
dialog.setContent(content);
dialog.setPosiContent(positiveContent);
dialog.setNegaContent(negativiContent);
dialog.setOnClickListener(onClickListener);
dialog.show();
}
}
以後每次調用就可以showDialog就可以了,這樣就可以更好的管理了