Android自定义控件——获取验证码倒计时

目前为止,有很多地方都需要绑定用户手机号,发送短信,比如说找回密码,注册或者登陆,这些控件都是很常用的,不需要每次使用的时候都重新写一个,这样比较麻烦,也浪费时间。本人自定义了一些控件

废话不多说,接下来就直奔主题,开始撸代码吧。

package com.morse.library.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.morse.library.R;
import com.morse.library.utils.CountDown;

/**
 * 带倒计时控件的+label+edittext的组合控件
 *
 * @company:大后天
 * @Author:曾明
 * @Time:2017/8/21 19:14
 * @Description:
 */
public class MLabelEditText extends LinearLayout implements View.OnClickListener {
    private TextView tvLabel;
    private EditText etEdit;
    private TextView tvCounter;
    private CountDown mCountDown;
    private long mTime;
    private long mInterval;

    private IRightClickListener mListener;

    public void setRightClickListener(IRightClickListener listener) {
        mListener = listener;
    }

    public MLabelEditText(Context context) {
        this(context, null);
    }

    public MLabelEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public MLabelEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
//        setGravity(Gravity.CENTER_VERTICAL);
//        setOrientation(HORIZONTAL);
//        setWillNotDraw(false);
        View view = LayoutInflater.from(context).inflate(R.layout.include_label_editview, this);
//        view.setBackgroundResource(R.drawable.shape_input_bg);
        tvLabel = (TextView) view.findViewById(R.id.lev_label);
        etEdit = (EditText) view.findViewById(R.id.lev_edit);
        tvCounter = (TextView) view.findViewById(R.id.lev_count);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MLabelEditView);
        String label = ta.getString(R.styleable.MLabelEditView_m_lev_label);
        String hint = ta.getString(R.styleable.MLabelEditView_m_lev_edit_hint);
        String edit = ta.getString(R.styleable.MLabelEditView_m_lev_edit);
        String counter = ta.getString(R.styleable.MLabelEditView_m_lev_count);
        int vCount = ta.getInt(R.styleable.MLabelEditView_m_lev_count_visibilty, 0);
        int vLabel = ta.getInt(R.styleable.MLabelEditView_m_lev_label_visibilty, 0);
        int color = ta.getInt(R.styleable.MLabelEditView_m_lev_count_color, Color.WHITE);
        int ems = ta.getInt(R.styleable.MLabelEditView_m_lev_ems, 5);
        ta.recycle();

        setLabel(label);
        setEditHint(hint);
        setEditText(edit);
        setCountText(counter);
        setCountVisibility(tvLabel, vLabel);
        setCountVisibility(tvCounter, vCount);
        setCountColor(color);
        setMinEms(ems);
        tvCounter.setOnClickListener(this);
    }

    /**
     * 设置右边控件的背景颜色
     *
     * @param color
     */
    public void setCountColor(int color) {
        if (color != -1) {
            tvCounter.setBackgroundResource(color);
        }
    }

    public void setTvCounterEnable(boolean isEnable){
        tvCounter.setEnabled(isEnable);
    }

    /**
     * 设置左边控件文本
     *
     * @param label
     */
    public void setLabel(String label) {
        if (!TextUtils.isEmpty(label)) {
            tvLabel.setText(label);
        }
    }

    /**
     * 设置EditText hint
     *
     * @param hint
     */
    public void setEditHint(String hint) {
        if (!TextUtils.isEmpty(hint)) {
            etEdit.setHint(hint);
        }
    }

    /**
     * 设置EditText 文本
     *
     * @param edit
     */
    public void setEditText(String edit) {
        if (!TextUtils.isEmpty(edit)) {
            etEdit.setText(edit);
            etEdit.setSelection(edit.length());
        }
    }

    /**
     * 设置右边控件文本
     *
     * @param counter
     */
    public void setCountText(String counter) {
        if (!TextUtils.isEmpty(counter)) {
            tvCounter.setText(counter);
        }
    }

    /**
     * 设置输入文本类型
     *
     * @param type
     */
    public void setInputType(int type) {
        etEdit.setInputType(type);
    }

    /**
     * 设置倒计时时长
     *
     * @param time
     */
    public void setTime(long time) {
        if (time > 0) {
            mTime = time;
        }
    }

    /**
     * 设置倒计时时间间隔
     *
     * @param interval
     */
    public void setInterval(long interval) {
        if (interval > 0) {
            mInterval = interval;
        }
    }

    /**
     * 设置右边控件的显示与隐藏
     * <p>0:显示</p>
     * <p>1:隐藏</p>
     * <p>2:隐藏,但控件还是占有空间</p>
     *
     * @param v
     */
    public void setCountVisibility(View view, int v) {
        if (v > 0) {
            switch (v) {
                case 0:
                    view.setVisibility(VISIBLE);
                    break;
                case 1:
                    view.setVisibility(GONE);
                    break;
                case 2:
                    view.setVisibility(INVISIBLE);
                    break;
                default:
                    view.setVisibility(VISIBLE);
                    break;
            }
        }
    }

    /**
     * 获取EditText文本
     *
     * @return
     */
    public String getText() {
        Editable editable = etEdit.getText();
        if (null == editable || TextUtils.isEmpty(editable.toString())) {
            return "";
        }
        return editable.toString();
    }

    public void setTextType() {

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.lev_count) {
            if (null != mListener) {
                mListener.onRightClick();
            }
            if (null == mCountDown) {
                mCountDown = new CountDown(getContext(), mTime, mInterval, tvCounter, 0, 0, 0);
            }
            mCountDown.start();
        }
    }

    public void setMinEms(int minEms) {
        if (minEms > 0) {
            tvLabel.setMinEms(minEms);
        }
    }

    /**
     * 右边控件点击事件
     */
    public interface IRightClickListener {
        void onRightClick();
    }
}

属性:

<declare-styleable name="MLabelEditView">
        <attr name="m_lev_label" format="string" />
        <attr name="m_lev_edit_hint" format="string" />
        <attr name="m_lev_count" format="string" />
        <attr name="m_lev_edit" format="string" />
        <attr name="m_lev_count_visibilty">
            <enum name="visiable" value="0" />
            <enum name="gone" value="1" />
            <enum name="invisiable" value="2" />
        </attr>
        <attr name="m_lev_label_visibilty">
            <enum name="visiable" value="0" />
            <enum name="gone" value="1" />
            <enum name="invisiable" value="2" />
        </attr>
        <attr name="m_lev_count_color" format="reference" />
        <attr name="m_lev_ems" format="integer" />
    </declare-styleable>

当然还需要倒计时:

package com.morse.library.utils;

import android.content.Context;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * 倒计时工具类
 *
 * @company:大后天
 * @Author:曾明
 * @Time:2017/8/23 21:06
 * @Description:
 */
public class CountDown extends CountDownTimer {

    private Context mContext;
    private View mView;
    private int mColor;
    private int mText;
    private int mFinishText;

    /**
     * 倒计时
     *
     * @param context
     * @param millisInFuture    倒计时时长
     * @param countDownInterval 倒计时时间间隔
     * @param view              倒计时控件
     * @param color             控件颜色
     * @param text              倒计时开始之后显示的文本
     * @param finishText        倒计时结束之后显示的文本
     */
    public CountDown(Context context, long millisInFuture, long countDownInterval, View view, int color, int text, int finishText) {
        super(millisInFuture, countDownInterval);
        mContext = context;
        mView = view;
        mColor = color;
        mText = text;
        mFinishText = finishText;
    }

    public CountDown(Context context, long millisInFuture, long countDownInterval, View view, int text, int finishText) {
        this(context, millisInFuture, countDownInterval, view, -1, text, finishText);
    }

    @Override
    public void onTick(long millisUntilFinished) {
        if (mView instanceof TextView || mView instanceof Button) {
            mView.setClickable(false);
            if (mColor > 0) {
                mView.setBackgroundColor(mContext.getResources().getColor(mColor));
            }
            if (mText <= 0) {
                return;
            }
            mView.setTag(mContext.getResources().getString(mText, millisUntilFinished / 1000));
        }
    }

    @Override
    public void onFinish() {
        if (mView instanceof TextView || mView instanceof Button) {
            mView.setClickable(true);
            if (mColor > 0) {
                mView.setBackgroundColor(mContext.getResources().getColor(mColor));
            }
            if (mFinishText <= 0) {
                return;
            }
            mView.setTag(mContext.getResources().getString(mFinishText));
        }
    }
}

注释已经很详细,具体的我就不再啰嗦了。希望对各位有所帮助。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值