您好,登錄后才能下訂單哦!
Android 仿支付寶的密碼均分輸入框
此為安卓項目,通過重繪edittext進行文字的均分排布。
直接貼上代碼:
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文件中設置好文字大小,設置好寬度。高度使用wrap_content更佳,亦可設置固定高度 * (隨著輸入的行數變化會導致高度成倍增加) * 允許設置每行顯示的文字個數 * 允許設置最多顯示多少行 * 允許設置密碼符顯示 * 允許設置多行輸入 * * Created by yueer on 2015/10/22. */ public class ExcelEditView extends EditText { private int mMaxLength = 6; //一行顯示的最大字符數 private int mColorId = Color.BLACK; //字體顏色 private boolean isPassword = false; //是否需要顯示密碼符 private float mHeight = 0.0f; //默認情況的高度 private int mMaxLine = 0; //最大的行數:如果為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(); //舊光標位置超過字符串長度 if(selEndIndex > newLen) { selEndIndex = editable.length(); } //設置新光標所在的位置 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(); //取出字符數組 int txtLine = getLineFromCharArray(txt); //計算有多少行 if (mMaxLine > 0 && txtLine > mMaxLine){ //進行行數的上限處理 txtLine = mMaxLine; } if (this.isPassword){ //密碼符的轉義 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); //動態設置控件高度 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); //進行繪制 } } } private int getLineFromCharArray(char[] txt){ int line = ((txt.length - 1) / mMaxLength) + 1; return line; } }
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。