在dialog實現中,如果就用系統自帶的顯示風格已經滿足不了大家的需求,那麼我們就需要自定義顯示的界面,那我們就直接把彈出框看成是一個View的界面(也就是一個佈局)我們可以在裏面去設計顯示內容;下面我們就來看下實現的代碼:
下面是在一個Activity中的一個打開Dialog的方法;
方法裏面調DialogViewsTypeAsk()時共傳了5個參數:
Context context,上下文
boolean showtwo,是否顯示兩個按鈕(及刪除和取消)
DialogViews_ask listener,兩個按鈕回調的接口
String content, 需要顯示的內容
String leftBtn, 左按鈕顯示名稱
String rightBtn右按鈕名稱
private void showDeleteLinkDialog(final TNPGetListRegisterAppOutput tnpBizLink) {
DialogViewsTypeAsk dialog = new DialogViewsTypeAsk(mView.getContext(), true,
new DialogViewsTypeAsk.DialogViews_ask() {
@Override
public void doOk() {
//操作
}
@Override
public void doCancel() {
//操作
}
}, "刪除小組成員",“ 刪除”,“取消”);
dialog.show();
}
在這裏我們的DialogViewsTypeAsk 類繼承了Dialog,在裏面自定義View來顯示你所想要的界面效果,裏面用到了DialogViews_ask 接口回調,直接把操作回調到調DialogViewsTypeAsk 的界面去處理;如果你想要設置Dialog的風格你可以通過super(context, R.style.dialog_normal);來設置;
public class DialogViewsTypeAsk extends Dialog {
// 確定
private Button btn_ok;
// 取消
private Button btn_cancle;
public DialogViews_ask mAction_ask;
public interface DialogViews_ask {
void doOk();
void doCancel();
}
public DialogViewsTypeAsk(Context context, boolean showtwo, DialogViews_ask listener,
String content, String leftBtn, String rightBtn) {
super(context, R.style.dialog_normal);
this.context = context;
setContentView(R.layout.dialog_ask);
this.mAction_ask = listener;
TextView tv_message = (TextView)findViewById(R.id.Tv_message_dialgask);
//TextView tv_title = (TextView)findViewById(R.id.Tv_title_dialgask);
tv_message.setVisibility(View.VISIBLE);
tv_title.setVisibility(View.GONE);
tv_message.setText(content);
// 實例化
// 確定
btn_ok = (Button)findViewById(R.id.Btn_okdialog_dialogask);
// 取消
btn_cancle = (Button)findViewById(R.id.Btn_cancledialog_dialogask);
btn_ok.setText(leftBtn);
btn_cancle.setText(rightBtn);
if (!showtwo) {
btn_cancle.setVisibility(View.GONE);
findViewById(R.id.line1).setVisibility(View.GONE);
findViewById(R.id.line2).setVisibility(View.GONE);
}
// 數據填充
setDataAndListener();
}
/**
* 數據填充和添加監聽
*/
private void setDataAndListener() {
// 確定
btn_ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
close();
if (mAction_ask != null) {
mAction_ask.doOk();
}
}
});
// 取消
btn_cancle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
close();
if (mAction_ask != null) {
mAction_ask.doCancel();
}
}
});
}
}
下面是R.layout.dialog_ask的佈局
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="20dip"
android:background="@drawable/org_pop_bg"
android:orientation="vertical" >
<!-- 標題 -->
<View
android:layout_width="15dip"
android:layout_height="15dip" />
<TextView
android:id="@+id/Tv_title_dialgask"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dip"
android:layout_marginLeft="15dip"
android:layout_marginRight="15dip"
android:gravity="center_vertical|center_horizontal"
android:minHeight="30dip"
android:textColor="@color/c12"
android:textSize="16sp" />
<TextView
android:id="@+id/Tv_message_dialgask"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dip"
android:layout_marginLeft="15dip"
android:layout_marginRight="15dip"
android:gravity="center_vertical|center_horizontal"
android:text=""
android:textColor="@color/c12"
android:textSize="16sp"
android:visibility="gone" />
<!-- 橫線 -->
<View
android:layout_width="fill_parent"
android:layout_height="0.3dip"
android:background="@color/guide_light_gray" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="39dip"
android:orientation="horizontal" >
<!-- 確定 -->
<Button
android:id="@+id/Btn_okdialog_dialogask"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:background="@drawable/dialog_ask_btn_selector"
android:gravity="center"
android:text="@string/finish"
android:textColor="@drawable/org_blue_black_select"
android:textSize="16sp" />
<View
android:id="@+id/line1"
android:layout_width="0.3dip"
android:layout_height="fill_parent"
android:background="@color/guide_light_gray" />
<!-- 取消 -->
<Button
android:id="@+id/Btn_cancledialog_dialogask"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:background="@drawable/dialog_ask_btn_selector"
android:gravity="center"
android:text="@string/cancel"
android:textColor="@drawable/org_blue_black_select"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
效果圖如下:
!!!!!不是完整的代碼,只爲總結,有錯的地方還望理解;