Android自定義通用對話框並實現按鈕事件接口回調

首先看一下對話框樣式效果如下:截圖不是很高清,湊合着看吧

下面是樣式文件代碼:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="296.0dip"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical|center"
    android:background="@drawable/ic_dlg_background" >

    <RelativeLayout
        android:id="@+id/dlg_rlyt_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="12.0dip" >

        <TextView
            android:id="@+id/id_dlg_common_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="我是標題"
            android:textColor="@color/dlg_title"
            android:textSize="18.0dip" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/dlg_rlyt_body"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/dlg_rlyt_title"
        android:paddingBottom="8.0dip" >

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:minHeight="88.0dip"
            android:paddingLeft="16.0dip"
            android:paddingRight="16.0dip"
            android:paddingTop="8.0dip" >

            <TextView
                android:id="@+id/id_dlg_common_content"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="20.0dip"
                android:gravity="left"
                android:text="您真的確定要退出嗎?"
                android:textColor="@color/dlg_content"
                android:textSize="16.0dip" />
        </RelativeLayout>

    </RelativeLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/dlg_rlyt_body"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="1.0px"
            android:background="@drawable/list_line_repeat" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="44.0dip" >

            <Button
                android:id="@+id/id_dlg_common_cancel"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/btn_left_selector"
                android:text="取消"
                android:textColor="@color/dlg_grey"
                android:textSize="16.0dip" />

            <ImageView
                android:layout_width="1.0px"
                android:layout_height="match_parent"
                android:background="@drawable/list_line_repeat" />

            <Button
                android:id="@+id/id_dlg_common_confirm"
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/btn_right_selector"
                android:text="確定"
                android:textColor="@color/dlg_blue"
                android:textSize="16.0dip" />
        </LinearLayout>
    </LinearLayout>

</RelativeLayout>
ThemeDialog繼承字AlertDialog,並定義了取消和確定按鈕的點擊事件的回調接口,代碼如下:

package org.hubu.yiyu.view;

import org.hubu.yiyu.R;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class ThemeDialog extends AlertDialog implements OnClickListener {

	private TextView tvTitle, tvContent;
	private Button btnCancel, btnConfirm;
	private ThemeDialogBtnListener listener;

	private Context context;
	private String title, content;
	private String cancel = "取消", confirm = "確定";

	public interface ThemeDialogBtnListener {
		public void onClick(View view);
	}

	public ThemeDialog(Context context, String title, String content,
			ThemeDialogBtnListener listener) {
		super(context);
		this.listener = listener;
		this.context = context;
		this.title = title;
		this.content = content;
	}

	public ThemeDialog(Context context, String title, String content,
			String cancel, String confirm, ThemeDialogBtnListener listener) {
		super(context);
		this.listener = listener;
		this.context = context;
		this.title = title;
		this.content = content;
		this.cancel = cancel;
		this.confirm = confirm;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dlg_common);
		initViews();
	}

	private void initViews() {
		tvTitle = (TextView) findViewById(R.id.id_dlg_common_title);
		tvContent = (TextView) findViewById(R.id.id_dlg_common_content);

		btnCancel = (Button) findViewById(R.id.id_dlg_common_cancel);
		btnConfirm = (Button) findViewById(R.id.id_dlg_common_confirm);

		tvTitle.setText(title);
		tvContent.setText(content);
		btnCancel.setText(cancel);
		btnConfirm.setText(confirm);

		btnCancel.setOnClickListener(this);
		btnConfirm.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.id_dlg_common_cancel:
			ThemeDialog.this.dismiss();
			break;

		default:
			break;
		}
		listener.onClick(v);
	}

}

調用示例:

首先實例化一個ThemeDialog的對象,然後實現對話框中的接口,並編寫兩個按鈕的點擊響應事件,代碼如下:

ThemeDialog themeDialog = new ThemeDialog(context, "提示",
						"確定放棄編輯?", new ThemeDialogBtnListener() {

							@Override
							public void onClick(View view) {
								switch (view.getId()) {
								case R.id.id_dlg_common_cancel:
									Log.i("我是取消按鈕的事件", "取消按鈕");
									break;
								case R.id.id_dlg_common_confirm:
									Log.i("我是確定按鈕的事件", "取消按鈕");
									break;
								default:
									break;
								}
							}
						});
				themeDialog.show();

至此便實現了一個通用的自定義對話框,對話框的樣式可根據系統需求做定製

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章