美文网首页
发送验证码布局

发送验证码布局

作者: 勤劳的蚂蚁 | 来源:发表于2020-03-11 13:47 被阅读0次
    image.png
    
    public class VerifyCodePpw extends PopupWindow {
        private View view;
        private Context mContext;
        private LinearLayout dismissTopLayout;
        private LinearLayout dismissBottomLayout;
        private TextView msgTv;
        private EditText codeEdt;
        private TextView resentCodeTv;
        private LinearLayout commitLayout;
        private OnVerifyCodeListener onVerifyCodeListener;
        private String phone;
        private String codeStr;
        private Handler mHandler= new Handler() {
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case 0:
                        resentCodeTv.setEnabled(false);
                        if (second < 10)
                            resentCodeTv.setText("0" + second + "s");
                        else
                            resentCodeTv.setText(second + "s");
                        break;
                    case 1:
                        resentCodeTv.setEnabled(true);
                        resentCodeTv.setText("重新发送");
                        break;
                }
            }
        };
    
        public void setOnVerifyCodeListener(OnVerifyCodeListener onVerifyCodeListener) {
            this.onVerifyCodeListener = onVerifyCodeListener;
        }
    
        public VerifyCodePpw(Activity context, String phone) {
            super(context);
            mContext = context;
            this.phone = phone;
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.verify_code_ppw, null);
            dismissTopLayout = ((LinearLayout) view.findViewById(R.id.verify_code_ppw_dismiss_top_ll));
            dismissBottomLayout = ((LinearLayout) view.findViewById(R.id.verify_code_ppw_dismiss_bottom_ll));
            msgTv = ((TextView) view.findViewById(R.id.verify_code_ppw_msg_tv));
            codeEdt = ((EditText) view.findViewById(R.id.verify_code_ppw_code_edt));
            resentCodeTv = ((TextView) view.findViewById(R.id.verify_code_ppw_resent_tv));
            commitLayout = ((LinearLayout) view.findViewById(R.id.verify_code_ppw_commit_ll));
            resentCodeTv.setEnabled(false);
            newTask();
            //设置SelectPicPopupWindow的View
            this.setContentView(view);
            //设置SelectPicPopupWindow弹出窗体的宽
            this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
            //设置SelectPicPopupWindow弹出窗体的高
            this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
            //设置SelectPicPopupWindow弹出窗体可点击
            this.setFocusable(true);
            //设置SelectPicPopupWindow弹出窗体动画效果
            this.setAnimationStyle(R.style.QRCodeAnim);
            //实例化一个ColorDrawable颜色为半透明
            ColorDrawable dw = new ColorDrawable(0000000000);
            //设置SelectPicPopupWindow弹出窗体的背景
            this.setBackgroundDrawable(dw);
            createParamater();
        }
    
        private void createParamater() {
            dismissTopLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onVerifyCodeListener != null){
                        onVerifyCodeListener.OnDisMissClick();
                        onVerifyCodeListener.OnDisMissView();
                    }
                    dismiss();
                }
            });
            dismissBottomLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onVerifyCodeListener != null){
                        onVerifyCodeListener.OnDisMissView();
                        onVerifyCodeListener.OnDisMissClick();
                    }
                    dismiss();
                }
            });
            initSpannableText();
            codeEdt.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
                }
    
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
    
                }
    
                @Override
                public void afterTextChanged(Editable s) {
                    codeStr = s.toString().trim();
                }
            });
            resentCodeTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onVerifyCodeListener != null){
                        newTask();
                        onVerifyCodeListener.OnReSentClick();
                    }
    
                }
            });
            commitLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //不输入验证码,code为空
                    if (TextUtils.isEmpty(codeStr)){
                        codeStr = "";
                        return;
                    }
                    if (onVerifyCodeListener != null){
                        onVerifyCodeListener.OnCommitClick(codeStr);
                    }
                }
            });
        }
    
        private void initSpannableText() {
            String content1 = "为了保证通话安全我们将向你的手机";
            String content2 = "发送一条短信,请注意查收";
            SpannableString text = new SpannableString(content1+phone.substring(3,6)+"****"+phone.substring(10,phone.length())+content2);
            ForegroundColorSpan span = new ForegroundColorSpan(Color.parseColor("#ff5722"));
            text.setSpan(span,content1.length(),content1.length()+11, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            msgTv.setText(text);
        }
        /**
         * 验证码倒计时
         */
        private int second = 120;
        private Timer timer;
        private TimerTask task;
    
        public void newTask() {
            second = 120;
            if (timer == null) {
                timer = new Timer();
            }
            if (task != null) {
                task.cancel();  //将原任务从队列中移除
            }
            task = new TimerTask() {
    
                @Override
                public void run() {
                    second--;
                    if (second <= 0) {
                        mHandler.sendEmptyMessage(1);
                    } else {
                        mHandler.sendEmptyMessage(0);
                    }
    
                }
            }; // 新建一个任务
            timer.scheduleAtFixedRate(task, 0, 1000);
    
        }
    
        public void show(View view){
            float dimen230=mContext.getResources().getDimension(R.dimen.dimen_176dp);
            showAtLocation(view,Gravity.CENTER,0,-(int)dimen230);
        }
    
    public interface OnVerifyCodeListener {
        void OnDisMissClick();
        void OnCommitClick(String codeStr);
        void OnReSentClick();
        void OnDisMissView();
    }
    }
    
    
    
    
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:background="#70000000"
        android:gravity="center"
        android:layout_height="match_parent">
        <LinearLayout
            android:orientation="horizontal"
            android:id="@+id/verify_code_ppw_dismiss_top_ll"
            android:layout_width="match_parent"
            android:layout_height="@dimen/dimen_0dp"
            android:layout_weight="1"/>
        <LinearLayout
            android:background="@drawable/bg_white_radius_8px"
            android:layout_marginLeft="@dimen/margin17"
            android:layout_marginRight="@dimen/margin17"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                android:id="@+id/verify_code_ppw_msg_tv"
                android:layout_marginTop="@dimen/margin32"
                android:layout_marginLeft="@dimen/margin32"
                android:layout_marginRight="@dimen/margin32"
                android:lineSpacingExtra="@dimen/dimen_18dp"
                android:textSize="@dimen/text16"
                android:textColor="@color/text666"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text=""/>
            <LinearLayout
                android:layout_marginTop="@dimen/dimen_36dp"
                android:layout_marginLeft="@dimen/margin32"
                android:layout_marginRight="@dimen/margin32"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <EditText
                    android:inputType="number"
                    android:id="@+id/verify_code_ppw_code_edt"
                    android:layout_weight="1"
                    android:textSize="@dimen/text16"
                    android:hint="请输入验证码"
                    android:textColorHint="@color/text999"
                    android:background="@null"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
                <TextView
                    android:id="@+id/verify_code_ppw_resent_tv"
                    android:textSize="@dimen/text16"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="重新发送"
                    android:textColor="@color/textff5722"/>
            </LinearLayout>
            <TextView
                android:layout_marginLeft="@dimen/margin32"
                android:layout_marginRight="@dimen/margin32"
                android:layout_marginTop="@dimen/dimen_10dp"
                android:layout_width="match_parent"
                android:layout_height="@dimen/line_width"
                android:background="@color/colorLine"/>
            <LinearLayout
                android:id="@+id/verify_code_ppw_commit_ll"
                android:layout_gravity="center"
                android:layout_marginTop="@dimen/margin20"
                android:layout_marginBottom="@dimen/margin20"
                android:background="@drawable/bg_ff5722_radius_10px"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <TextView
                    android:gravity="center"
                    android:layout_width="@dimen/dimen_124dp"
                    android:layout_height="@dimen/dimen_34dp"
                    android:text="确定"
                    android:textSize="@dimen/text17"/>
            </LinearLayout>
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal"
            android:id="@+id/verify_code_ppw_dismiss_bottom_ll"
            android:layout_width="match_parent"
            android:layout_height="@dimen/dimen_0dp"
            android:layout_weight="1"/>
    </LinearLayout>
    
    bg_white_radius_8px.xml
    
    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <corners android:radius="8px"/>
        <solid android:color="@color/white"/>
    </shape>
    

    相关文章

      网友评论

          本文标题:发送验证码布局

          本文链接:https://www.haomeiwen.com/subject/lffcjhtx.html