android自定义Dialog,输入支付密码,带参数回调

2019-04-27  本文已影响0人  心中有梦丶身边有你

自定义工具类

public class PayPurchaDialog extends Dialog implements View.OnClickListener {

    private EditText mEtPwd;
    private TextView mTvCancel;
    private TextView mTvOk;

    public PayPurchaDialog(@NonNull Context context) {
        this(context, 0);
    }

    public PayPurchaDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        View view = View.inflate(context, R.layout.dialog_pay_pwd, null);
        mEtPwd = view.findViewById(R.id.dialog_pay_pwd_et_pwd);
        view.findViewById(R.id.dialog_pay_pwd_tv_cancel).setOnClickListener(this);
        view.findViewById(R.id.dialog_pay_pwd_tv_ok).setOnClickListener(this);
        setCancelable(false);
        setContentView(view);
        changeDialogStyle();
    }

    /**
     * 设置dialog占满屏幕
     */
    private void changeDialogStyle() {
        Window window = getWindow();
        if (window != null) {
            WindowManager.LayoutParams attr = window.getAttributes();
            if (attr != null) {
                attr.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                attr.width = ViewGroup.LayoutParams.MATCH_PARENT;
                window.setAttributes(attr);
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.dialog_pay_pwd_tv_cancel:
                dismiss();
                break;
            case R.id.dialog_pay_pwd_tv_ok:
                String payPwd = mEtPwd.getText().toString();
                if (!TextUtils.isEmpty(payPwd)) {
                    if (mOnOkClick != null) {
                        mOnOkClick.onOkClick(payPwd);
                    }
                }
                dismiss();
                break;
        }
    }
    public void setOnOkClick(OnOkClick onOkClick){
        mOnOkClick = onOkClick;
    }
    private OnOkClick mOnOkClick;
    public interface OnOkClick{
        void onOkClick(String payPwd);
    }

dialog_pay_pwd.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:background="#0000">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/dialog_activate_shape"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="6dp"
            android:textSize="22sp"
            android:text="交易密码"
            android:textColor="#AF00"/>

        <EditText
            android:id="@+id/dialog_pay_pwd_et_pwd"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:layout_marginTop="12dp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            android:inputType="textPassword"
            android:gravity="center"
            android:paddingLeft="2dp"
            android:paddingRight="2dp"
            android:paddingTop="8dp"
            android:paddingBottom="8dp"
            android:background="@drawable/shape_dialog_pay_pwd_et_bg"
            android:hint="请输入交易密码"
            android:textColor="#6000"/>

        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:background="#C0C0C0"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:orientation="horizontal">


            <TextView
                android:id="@+id/dialog_pay_pwd_tv_cancel"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:gravity="center"
                android:text="取消" />

            <View
                android:layout_width="1px"
                android:layout_height="wrap_content"
                android:background="#C0C0C0"/>
            <TextView
                android:id="@+id/dialog_pay_pwd_tv_ok"
                android:gravity="center"
                android:layout_width="0dp"
                android:textColor="#AF00"
                android:layout_gravity="center"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="确定"/>

        </LinearLayout>
    </LinearLayout>

</LinearLayout>

Activity中回调 (写在点击事件中)

PayPurchaDialog dialog = new PayPurchaDialog(mContext);//支付密码弹出框
                dialog.setOnOkClick(new PayPurchaDialog.OnOkClick() {
                    @Override
                    public void onOkClick(String payPwd) {
                        //TODO:点击确定, 密码回调在这里  先判断密码,然后调用接口
                         dialog.dismiss(); //关闭窗口
                      //带着密码payPwd调用你的方法
                    
                    }
                });
                dialog.show();
上一篇下一篇

猜你喜欢

热点阅读