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

这个主要是实现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);








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值