关闭

Android 对话框封装(确认/取消 )

736人阅读 评论(0) 收藏 举报
分类:

先看效果图:



就是这样的一个简单的问答对话框,防止用户误操作,可能要多处使用,可以封装一下。

第一步:布局文件 dialog.xml

<?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="wrap_content"
    android:gravity="center_horizontal"
    android:background="@null"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#fff"
        android:orientation="vertical"
        android:padding="10dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/ivIcon"
                android:layout_width="50dp"
                android:layout_height="50dp" />

            <TextView
                android:id="@+id/tvText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:text="New TextNet" />
        </LinearLayout>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="horizontal">

            <Button
                android:id="@+id/btnCancel"
                android:layout_width="100dp"
                android:layout_height="40dp"
                android:layout_below="@+id/tvContent"
                android:background="#DEDEDE"
                android:text="取消" />

            <View
                android:layout_width="10dp"
                android:layout_height="0dp" />

            <Button
                android:id="@+id/btnSure"
                android:layout_width="100dp"
                android:layout_height="40dp"
                android:layout_alignParentRight="true"
                android:layout_below="@+id/tvContent"
                android:background="#3BB7B8"
                android:text="确认" />
        </LinearLayout>
    </LinearLayout>


</LinearLayout>

第二步:接口类 DialogButtonListener.java

/**
 * Developer : xiongwenwei@aliyun.com
 * Create Time :2016-5-31 10:14:19
 * Function:对话框接口类
 */
public interface DialogButtonListener {

    public void sure();//确认键
    public void cancel();//取消键
}

第三步:Dialog工具类 DialogUtil.java

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.xiong.autodetail.R;
import com.xiong.autodetail.activity.BaseActivity;

/**
 * Developer : xiongwenwei@aliyun.com
 * Create Time :2016-5-31 15:01:50
 * Function:确认操作对话框
 */
public class DialogUtil {

    private AlertDialog dlg;
    private ImageView ivIcon;
    private TextView tvText;
    private Button btnCancel,btnSure;

    private Context context;
    private int imgResId = 0;
    private String text;
    private DialogButtonListener listener;

    public void show(String text, final DialogButtonListener listener) {
        this.context = BaseActivity.getInstance();
        this.text = text;
        this.listener = listener;
        createDialog();
        setValue();
    }

    public void show( int imgResId, String text, final DialogButtonListener listener) {
        this.context = BaseActivity.getInstance();
        this.text = text;
        this.listener = listener;
        this.imgResId = imgResId;
        createDialog();
        setValue();
    }

    public void show(Context context, String text, final DialogButtonListener listener) {
        this.context = context;
        this.text = text;
        this.listener = listener;
        createDialog();
        setValue();
    }

    public void show(Context context, int imgResId, String text, final DialogButtonListener listener) {
        this.context = context;
        this.text = text;
        this.listener = listener;
        this.imgResId = imgResId;
        createDialog();
        setValue();
    }

    //创建Dialog、初始化控件
    private void createDialog() {
        dlg = new AlertDialog.Builder(context).create();
        dlg.show();
        Window window = dlg.getWindow();
        window.setContentView(R.layout.dialog);
        window.setGravity(Gravity.CENTER);//居中
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));//背景透明
        ivIcon = (ImageView) window.findViewById(R.id.ivIcon);
        tvText = (TextView) window.findViewById(R.id.tvText);
        btnCancel = (Button) window.findViewById(R.id.btnCancel);
        btnSure = (Button) window.findViewById(R.id.btnSure);
    }

    //设置控件值
    private void setValue() {
        if (imgResId != 0) {
            ivIcon.setImageResource(imgResId);
        } else {
            ivIcon.setVisibility(View.GONE);
        }
        tvText.setText(text);
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.cancel();
                dlg.dismiss();
            }
        });
        btnSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.sure();
                dlg.dismiss();
            }
        });
    }
}
第四步:代码调用
DialogUtil dialogUtil = new DialogUtil();
        dialogUtil.show(R.drawable.idea, "确认修改吗?", new DialogButtonListener() {
            @Override
            public void sure() {
                ToastUtil.show("点击了确认");
            }

            @Override
            public void cancel() {
                ToastUtil.show("点击了取消");
            }
        });

其它类 BaseActivity.java

private static BaseActivity instance = null;

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);

        instance = this;
    }

    public static BaseActivity getInstance() {
        return instance;
    }

END !







0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:80464次
    • 积分:1787
    • 等级:
    • 排名:千里之外
    • 原创:108篇
    • 转载:1篇
    • 译文:0篇
    • 评论:31条
    最新评论