Android自定义通用对话框并实现按钮事件接口回调

本文展示了如何在Android中创建一个自定义的通用对话框,详细讲解了实现对话框样式以及设置按钮点击事件回调的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先看一下对话框样式效果如下:截图不是很高清,凑合着看吧

下面是样式文件代码:

<?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();

至此便实现了一个通用的自定义对话框,对话框的样式可根据系统需求做定制

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值