您好,登錄后才能下訂單哦!
Android 仿支付寶的密碼均分輸入框
此為安卓項目,通過重繪edittext進(jìn)行文字的均分排布。
直接貼上代碼:
package com.xxx.xxx; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.text.Editable; import android.text.Selection; import android.text.TextWatcher; import android.util.AttributeSet; import android.view.ViewGroup; import android.widget.EditText; /** * 此控件為均分輸入框控件 * 使用說明:XML文件中設(shè)置好文字大小,設(shè)置好寬度。高度使用wrap_content更佳,亦可設(shè)置固定高度 * (隨著輸入的行數(shù)變化會導(dǎo)致高度成倍增加) * 允許設(shè)置每行顯示的文字個數(shù) * 允許設(shè)置最多顯示多少行 * 允許設(shè)置密碼符顯示 * 允許設(shè)置多行輸入 * * Created by yueer on 2015/10/22. */ public class ExcelEditView extends EditText { private int mMaxLength = 6; //一行顯示的最大字符數(shù) private int mColorId = Color.BLACK; //字體顏色 private boolean isPassword = false; //是否需要顯示密碼符 private float mHeight = 0.0f; //默認(rèn)情況的高度 private int mMaxLine = 0; //最大的行數(shù):如果為0,---表示支持多行輸入 不為0,--則為該行 public ExcelEditView(Context context){ super(context); init(); } public ExcelEditView(Context context, AttributeSet set){ super(context, set); init(); } private void init(){ this.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) { // TODO Auto-generated method stub Editable editable = ExcelEditView.this.getText(); int len = editable.length(); if(mMaxLine > 0 && len > mMaxLength*mMaxLine) { int selEndIndex = Selection.getSelectionEnd(editable); String str = editable.toString(); String newStr = str.substring(0,mMaxLength*mMaxLine); ExcelEditView.this.setText(newStr); editable = ExcelEditView.this.getText(); //新字符串的長度 int newLen = editable.length(); //舊光標(biāo)位置超過字符串長度 if(selEndIndex > newLen) { selEndIndex = editable.length(); } //設(shè)置新光標(biāo)所在的位置 Selection.setSelection(editable, selEndIndex); } } @Override public void afterTextChanged(Editable s) { } }); } public void setIsPassword(boolean isPassword){ this.isPassword = isPassword; } public void setmMaxLine(int line){ this.mMaxLine = line; } public void setmMaxLength(int leng){ this.mMaxLength = leng; } @Override public void setTextColor(int color) { super.setTextColor(color); mColorId = color; } @Override protected void onDraw(Canvas canvas) { char[] txt = this.getText().toString().toCharArray(); //取出字符數(shù)組 int txtLine = getLineFromCharArray(txt); //計算有多少行 if (mMaxLine > 0 && txtLine > mMaxLine){ //進(jìn)行行數(shù)的上限處理 txtLine = mMaxLine; } if (this.isPassword){ //密碼符的轉(zhuǎn)義 for (int i=0; i<txt.length; i++){ txt[i] = '*'; } } if (mHeight == 0){ //獲取最初控件的高度 mHeight = this.getHeight(); } float width = this.getWidth(); float height = mHeight * txtLine; ViewGroup.LayoutParams params = this.getLayoutParams(); params.height = (int)height; this.setLayoutParams(params); //動態(tài)設(shè)置控件高度 float per = width / (mMaxLength+1); //寬度等分 float perHeight = height / (txtLine + 1); //高度等分 Paint countPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG); countPaint.setColor(mColorId); countPaint.setTextSize(this.getTextSize()); countPaint.setTypeface(this.getTypeface()); countPaint.setTextAlign(Paint.Align.CENTER); Rect textBounds = new Rect(); String numberStr = "1"; countPaint.getTextBounds(numberStr, 0, numberStr.length(), textBounds);//get text bounds, that can get the text width and height float textHeight = (float)(textBounds.bottom - textBounds.top); float textWidth = (float)(textBounds.right = textBounds.left); //計算該控件中能夠顯示的單一文字的高度和寬度 for (int line = 0; line < txtLine; line++) { for (int i = 0; i < mMaxLength && txt.length > (i+line*mMaxLength); i++) { canvas.drawText(String.valueOf(txt[i+line*mMaxLength]), (i + 1) * per - textWidth, perHeight * (line + 1) + textHeight / 2, countPaint); //進(jìn)行繪制 } } } private int getLineFromCharArray(char[] txt){ int line = ((txt.length - 1) / mMaxLength) + 1; return line; } }
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。