android EditText输入四位数字密码明文显示

来源:互联网 发布:csol死神辅助源码 编辑:程序博客网 时间:2024/06/05 15:55

这个主要是实现Edittext连续明文显示数字输入,数字个数可随意修改

效果图:



利用ondraw的方式实现:

public class PasswordInputView extends EditText {private static String TAG = PasswordInputView.class.getName();    private int passwordLength = 4;    private Paint passwordPaint = new Paint(ANTI_ALIAS_FLAG);    private int numsLength;    private String nums;    private int numwidth;        public PasswordInputView(Context context, AttributeSet attrs) {        super(context, attrs);                passwordPaint.setStyle(Paint.Style.FILL);        passwordPaint.setColor(Color.BLACK);    }    @Override    protected void onDraw(Canvas canvas) {        int width = getWidth();        int height = getHeight();                float cx, cy = height/ 2;        float half = width / passwordLength / 2;        float everyWidth =  width / passwordLength;        float lineSpce = ConstVar.xZoom * 22;                for(int i = 0; i < passwordLength; i++) {        canvas.drawRect(lineSpce + everyWidth * i, height-2, everyWidth * (i+1) - lineSpce, height, passwordPaint);        }                for(int i = 0; i < numsLength; i++) {            cx = width * i / passwordLength + half;            String txt = nums.substring(i, i+1);            this.numwidth = (int) Math.ceil(passwordPaint.measureText(txt));             canvas.drawText(txt,cx - (numwidth / 2) , cy + (numwidth / 2), passwordPaint);                    }            }    @Override    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {        super.onTextChanged(text, start, lengthBefore, lengthAfter);        this.numsLength = text.toString().length();        this.nums = text.toString();        DebugUtils.debug(TAG, "text.toString() " + text.toString() );                invalidate();    }//    private int getPasswordLength() {//        return passwordLength;//    }////    private void setPasswordLength(int passwordLength) {//        this.passwordLength = passwordLength;//        invalidate();//    }    public void setPasswordPaintSize() {        passwordPaint.setTextSize(ConstVar.xZoom * 60);        DebugUtils.debug(TAG, "setPasswordPaintSize size: " + ConstVar.xZoom * 60);    }}

在初始化布局的地方调用

PasswordInputView  num_edit = (PasswordInputView) view.findViewById(R.id.passwordInputView);num_edit.setPasswordPaintSize();

ConstVar.xZoom 是手机的缩放比率,通过手机的分辨率计算

DisplayMetrics dm = new DisplayMetrics(); // 获取屏幕分辨率((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(dm);int screenWidth = dm.widthPixels; // 水平分辨率int screenHeight = dm.heightPixels; // 垂直分辨率int tempHeight = screenHeight;int tempWidth = screenWidth;if (tempHeight > tempWidth) { // 若是竖屏,则仍为竖屏screenHeight = tempHeight;screenWidth = tempWidth;} else {screenHeight = tempWidth; // 否则将当前屏幕转为竖屏screenWidth = tempHeight;}// 得到缩放比率ConstVar.screenWidth = screenWidth;ConstVar.screenHeight = screenHeight;ConstVar.xZoom = (float) (screenWidth / ConstVar.consWidth);ConstVar.yZoom = (float) (screenHeight / ConstVar.consHeight);








原创粉丝点击