安卓开发:自定义双向选择确认框(“确认”“取消”提示)

先看效果
完成的效果图
看到这个就想到系统提供的Dialog,但是各个版本的又不一样,有的很难看,so…继承他们,自定义一个
先写一下自己需要的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="280dp"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:orientation="vertical"
        android:paddingLeft="10dp">

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"
            android:padding="5dp"
            android:text="@string/prompt"
            android:textColor="#505050"
            android:textSize="18dp"
            android:visibility="visible" />

        <TextView
            android:id="@+id/alert_message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp"
            android:layout_marginTop="20dp"
            android:gravity="center_horizontal"
            android:padding="5dp"
            android:textColor="#505050"
            android:textSize="16dp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:gravity="right"
        android:orientation="horizontal"
        android:paddingBottom="8dp">

        <Button
            android:id="@+id/btn_cancel"
            style="?android:attr/borderlessButtonStyle"
            android:layout_width="65dp"
            android:layout_height="32dp"
            android:background="@drawable/ease_btn_cancel_bj"
            android:gravity="center"
            android:text="@string/cancel"
            android:textColor="#969696"
            android:textSize="15dp" />

        <Button
            android:id="@+id/btn_ok"
            style="?android:attr/borderlessButtonStyle"
            android:layout_width="65dp"
            android:layout_height="32dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="10dp"
            android:background="@drawable/ease_btn_cancel_bj"
            android:gravity="center"
            android:text="@string/ok"
            android:textColor="#09aaf5"
            android:textSize="15dp" />
    </LinearLayout>

</LinearLayout>

效果就是这样的
这里写图片描述

然后写逻辑代码,新起一个类,继承自Dialog

class EaseAlertDialog extends Dialog

声明需要的字段

     private String title;//标题
     private String msg;//提示消息
     private CallBack callBack;
     private boolean showCancel = false;//是否显示取消按钮
     private Bundle bundle;//扩展字段

定义一个按钮点击回调接口

public interface CallBack {
       void onResult(boolean confirmed, Bundle bundle);
    }

编写构造方法(可以写多个,通过不同的参数来控制不同显示方式)

//通用的构造方法
public EaseAlertDialog(Context context, String title, String msg, Bundle bundle, CallBack callBack, boolean showCancel) {
        super(context);
        this.title = title;
        this.msg = msg;
        this.callBack = callBack;
        this.bundle = bundle;
        this.showCancel = showCancel;
        this.setCanceledOnTouchOutside(true);//点击外部是否可关闭
    }
//只显示一个取消按钮,不可点击外部关闭

public EaseAlertDialog(Context context, String msg, AlertDialogUser user, boolean showCancel) {
super(context);
this.msg = msg;
this.user = user;
this.showCancel = showCancel;
this.setCanceledOnTouchOutside(false);//设置点击外部不可关闭
}

重写onCreate方法,绑定布局,初始化控件,设置按钮点击事件
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.ease_alert_dialog);//绑定布局
        //声明控件
        Button cancel = (Button) findViewById(R.id.btn_cancel);
        Button ok = (Button) findViewById(R.id.btn_ok);
        TextView titleView = (TextView) findViewById(R.id.title);
        TextView msgView = (TextView) findViewById(R.id.alert_message);

        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (view.getId() == R.id.btn_ok) {
                    onOk(view);
                } else if (view.getId() == R.id.btn_cancel) {
                    onCancel(view);
                }
            }
        };
        //设置按钮点击事件
        cancel.setOnClickListener(listener);
        ok.setOnClickListener(listener);
        //设置标题
        if (title != null) {
            titleView.setText(title);
        } else {
            titleView.setVisibility(View.GONE);
        }
        //设置提示内容
        if (msg != null){
            msgView.setText(msg);
        }else{
            msgView.setVisibity(View.GONE);
        }
        //是否显示取消按钮
        if (showCancel) {
            cancel.setVisibility(View.VISIBLE);
        } else {
            cancel.setVisibility(View.GONE);
        }
    }

    public void onOk(View view) {
        this.dismiss();
        if (this.user != null) {
            this.user.onResult(true, this.bundle);
        }
    }

    public void onCancel(View view) {
        this.dismiss();
        if (this.user != null) {
            this.user.onResult(false, this.bundle);
        }
    }

好了,结束了,等会附上源码下载链接


源码下载链接:http://download.csdn.net/download/ximen_qing/9955555

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值