国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 實現仿支付寶的密碼均分輸入框

2019-12-12 02:34:15
字體:
來源:轉載
供稿:網友

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;  }}

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 哈密市| 恩平市| 云梦县| 南通市| 太和县| 和政县| 沾化县| 临泉县| 宜章县| 洞口县| 运城市| 临清市| 伊吾县| 油尖旺区| 宝坻区| 太仆寺旗| 岐山县| 牡丹江市| 道真| 子长县| 仁布县| 贡嘎县| 会昌县| 青田县| 昭平县| 广德县| 凌云县| 平乡县| 台北市| 互助| 公主岭市| 桐庐县| 璧山县| 孙吴县| 明水县| 五家渠市| 平定县| 武清区| 孟村| 桐乡市| 星子县|