GVKun编程网logo

Android仿微信/支付宝密码输入框

122

想了解Android仿微信/支付宝密码输入框的新动态吗?本文将为您提供详细的信息,此外,我们还将为您介绍关于Android之EditText为密码输入框时,密码的显示与隐藏、android代码实现密码

想了解Android仿微信/支付宝密码输入框的新动态吗?本文将为您提供详细的信息,此外,我们还将为您介绍关于Android 之 EditText 为密码输入框时,密码的显示与隐藏、android 代码实现密码输入框、Android 仿支付宝密码输入框效果、Android 实现仿支付宝的密码均分输入框的新知识。

本文目录一览:

Android仿微信/支付宝密码输入框

Android仿微信/支付宝密码输入框

在用到支付类app时,都有一个简密的输入框。。开始实现的时候思路有点问题,后来到github上搜了下,找到了一个开源的库看起来相当的牛逼,,来个地址先:

https://github.com/Jungerr/GridPasswordView

效果图:

这个开源库我研究了之后,又有了自己的一个思路:来个假的简密框---底部放一个EditTextView,顶部放置6个ImageView的原点,控制他们的显隐来实现这个简密宽

开发步骤:

1 布局

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_height="50dp" > 
 <LinearLayout 
  android:baselineAligned="false" 
  android:layout_width="match_parent" 
  android:layout_height="50dp" 
android:background="@drawable/sdk2_simple_pwd_bg_" 
  android:orientation="horizontal" > 
  <RelativeLayoutandroid:layout_weight="1" 
   android:orientation="horizontal" > 
   <ImageView 
    android:id="@+id/sdk2_pwd_one_img"android:layout_centerInParent="true" 
android:src="@drawable/sdk_circle_icon" 
    android:visibility="invisible" /> 
   <View 
    android:layout_width="1dp" 
    android:layout_height="fill_parent" 
android:layout_alignParentRight="true"  android:background="@color/sdk_color_pwd_line" /> 
  </RelativeLayout> 
  <RelativeLayoutandroid:layout_weight="1" 
   android:orientation="horizontal" > 
   <ImageView 
    android:id="@+id/sdk2_pwd_two_img"android:layout_centerInParent="true" 
android:src="@drawable/sdk_circle_icon" 
    android:visibility="invisible" /> 
   <View 
    android:layout_width="1dp" 
    android:layout_height="fill_parent" 
 android:layout_alignParentRight="true"  android:background="@color/sdk_color_pwd_line" /> 
  </RelativeLayout> 
  <RelativeLayoutandroid:layout_weight="1" 
   android:orientation="horizontal" > 
   <ImageView 
 android:id="@+id/sdk2_pwd_three_img"android:layout_centerInParent="true" 
  android:src="@drawable/sdk_circle_icon" 
    android:visibility="invisible" /> 
   <View 
    android:layout_width="1dp" 
    android:layout_height="fill_parent" 
  android:layout_alignParentRight="true" 
 android:background="@color/sdk_color_pwd_line" /> 
  </RelativeLayout> 
  <RelativeLayoutandroid:layout_weight="1" 
   android:orientation="horizontal" > 
   <ImageView 
    android:id="@+id/sdk2_pwd_four_img"android:layout_centerInParent="true" 
 android:src="@drawable/sdk_circle_icon" 
    android:visibility="invisible" /> 
   <View 
    android:layout_width="1dp" 
    android:layout_height="fill_parent" 
 android:layout_alignParentRight="true"  android:background="@color/sdk_color_pwd_line" /> 
  </RelativeLayout> 
  <RelativeLayoutandroid:layout_weight="1" 
   android:orientation="horizontal" > 
   <ImageView 
    android:id="@+id/sdk2_pwd_five_img"android:layout_centerInParent="true" 
android:src="@drawable/sdk_circle_icon" 
    android:visibility="invisible" /> 
   <View 
    android:layout_width="1dp" 
    android:layout_height="fill_parent" 
  android:layout_alignParentRight="true" 
    android:background="@color/sdk_color_pwd_line" /> 
  </RelativeLayout> 
  <RelativeLayoutandroid:layout_weight="1" 
   android:orientation="horizontal" > 
   <ImageView    android:id="@+id/sdk2_pwd_six_img"android:layout_centerInParent="true"     android:src="@drawable/sdk_circle_icon" 
    android:visibility="invisible" /> 
   <View 
android:layout_width="1dp"    android:layout_height="fill_parent"     android:layout_alignParentRight="true"    android:background="@color/sdk_color_pwd_line" /> 
  </RelativeLayout> 
 </LinearLayout> 
 <EditText 
  android:id="@+id/sdk2_pwd_edit_simple"android:background="@null" 
  android:cursorVisible="false" 
  android:inputType="numberPassword" 
  android:maxLength="6" 
 android:textColor="@color/sdk2_color_black" /> 
</FrameLayout> 

2:自定义一个控件来处理输入、删除、显隐等事件

package com.suning.mobile.paysdk.view; 
import android.content.Context; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.util.AttributeSet; 
import android.view.KeyEvent; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import com.suning.mobile.paysdk.R; 
import com.suning.mobile.paysdk.utils.FunctionUtils; 
import com.suning.mobile.paysdk.utils.log.LogUtils; 
/** 
 * 
 * 〈一句话功能简述〉<br> 
 * 〈功能详细描述〉 简密输入框 
 */ 
public class SecurityPasswordEditText extends LinearLayout { 
 private EditText mEditText; 
 private ImageView oneTextView; 
 private ImageView twoTextView; 
 private ImageView threeTextView; 
 private ImageView fourTextView; 
 private ImageView fiveTextView; 
 private ImageView sixTextView; 
 LayoutInflater inflater; 
 ImageView[] imageViews; 
 View contentView; 
 public SecurityPasswordEditText(Context context,AttributeSet attrs) { 
  super(context,attrs); 
  inflater = LayoutInflater.from(context); 
  builder = new StringBuilder(); 
  initWidget(); 
 } 
 private void initWidget() { 
  contentView = inflater.inflate(R.layout.sdk_simple_pwd_widget,null); 
  mEditText = (EditText) contentView 
    .findViewById(R.id.sdk_pwd_edit_simple); 
  oneTextView = (ImageView) contentView 
    .findViewById(R.id.sdk_pwd_one_img); 
  twoTextView = (ImageView) contentView 
    .findViewById(R.id.sdk_pwd_two_img); 
  fourTextView = (ImageView) contentView 
    .findViewById(R.id.sdk_pwd_four_img); 
  fiveTextView = (ImageView) contentView 
    .findViewById(R.id.sdk_pwd_five_img); 
  sixTextView = (ImageView) contentView 
    .findViewById(R.id.sdk_pwd_six_img); 
  threeTextView = (ImageView) contentView 
    .findViewById(R.id.sdk_pwd_three_img); 
  LinearLayout.LayoutParams lParams = new LayoutParams( 
    LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT); 
  mEditText.addTextChangedListener(mTextWatcher); 
  mEditText.setonKeyListener(keyListener); 
  imageViews = new ImageView[] { oneTextView,twoTextView,threeTextView,fourTextView,fiveTextView,sixTextView }; 
  this.addView(contentView,lParams); 
 } 
 TextWatcher mTextWatcher = new TextWatcher() { 
  @Override 
  public void onTextChanged(CharSequence s,int start,int before,int count) { 
  } 
  @Override 
  public void beforeTextChanged(CharSequence s,int count,int after) { 
  } 
  @Override 
  public void afterTextChanged(Editable s) { 
   if (s.toString().length() == ) { 
    return; 
   } 
   if (builder.length() < ) { 
    builder.append(s.toString()); 
    setTextValue(); 
   } 
   s.delete(,s.length()); 
  } 
 }; 
 OnKeyListener keyListener = new OnKeyListener() { 
  @Override 
  public boolean onKey(View v,int keyCode,KeyEvent event) { 
   if (keyCode == KeyEvent.KEYCODE_DEL 
     && event.getAction() == KeyEvent.ACTION_UP) { 
    delTextValue(); 
    return true; 
   } 
   return false; 
  } 
 }; 
 private void setTextValue() { 
  String str = builder.toString(); 
  int len = str.length(); 
  if (len <= ) { 
   imageViews[len - ].setVisibility(View.VISIBLE); 
  } 
  if (len == ) { 
   LogUtils.i("回调"); 
   LogUtils.i("支付密码" + str); 
   if (mListener != null) { 
    mListener.onNumCompleted(str); 
   } 
   LogUtils.i("jone",builder.toString()); 
   FunctionUtils.hideSoftInputByView(getContext(),mEditText); 
  } 
 } 
 private void delTextValue() { 
  String str = builder.toString(); 
  int len = str.length(); 
  if (len == ) { 
   return; 
  } 
  if (len > && len <= ) { 
   builder.delete(len -,len); 
  } 
  imageViews[len - ].setVisibility(View.INVISIBLE); 
  ; 
 } 
 StringBuilder builder; 
 public interface SecurityEditCompleListener { 
  public void onNumCompleted(String num); 
 } 
 public SecurityEditCompleListener mListener; 
 public void setSecurityEditCompleListener( 
   SecurityEditCompleListener mListener) { 
  this.mListener = mListener; 
 } 
 public void clearSecurityEdit() { 
  if (builder != null) { 
   if (builder.length() == ) { 
    builder.delete(,); 
   } 
  } 
  for (ImageView tv : imageViews) { 
   tv.setVisibility(View.INVISIBLE); 
  } 
 } 
 public EditText getSecurityEdit() { 
  return this.mEditText; 
 } 
} 

这样子其实也实现了简密功能,但是这个比前面那个开源库简单了许多,当然功能也没有前面的那个强大。

以上内容给大家介绍了Android仿微信/支付宝密码输入框的全部叙述,希望大家喜欢。

Android 之 EditText 为密码输入框时,密码的显示与隐藏

Android 之 EditText 为密码输入框时,密码的显示与隐藏

实现输入框密码文本的显示与隐藏有两种,一种是通过直接改变 android:inputType,一种是通过改变 android.text.method.TransformationMethod


方式一:改变 android:inputType 的值


实现代码如下:

/**
	 * 密码显示或隐藏 (切换)
	 */
	private void showOrHide(EditText etPassword){
		//记住光标开始的位置
		int pos = etPassword.getSelectionStart();
		if(etPassword.getInputType()!= (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD)){//隐藏密码
			etPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
		}else{//显示密码
			etPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
		}
		etPassword.setSelection(pos);
		
	}

参见:android:inputType

对应的值与描述如下:

Constant Value Description
none 0x00000000 There is no content type. The text is not editable.
text 0x00000001 Just plain old text. Corresponds to TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_NORMAL.
textCapCharacters 0x00001001 Can be combined with text and its variations to request capitalization of all characters. Corresponds to TYPE_TEXT_FLAG_CAP_CHARACTERS.
textCapWords 0x00002001 Can be combined with text and its variations to request capitalization of the first character of every word. Corresponds to TYPE_TEXT_FLAG_CAP_WORDS.
textCapSentences 0x00004001 Can be combined with text and its variations to request capitalization of the first character of every sentence. Corresponds to TYPE_TEXT_FLAG_CAP_SENTENCES.
textAutoCorrect 0x00008001 Can be combined with text and its variations to request auto-correction of text being input. Corresponds to TYPE_TEXT_FLAG_AUTO_CORRECT.
textAutoComplete 0x00010001 Can be combined with text and its variations to specify that this field will be doing its own auto-completion and talking with the input method appropriately. Corresponds toTYPE_TEXT_FLAG_AUTO_COMPLETE.
textMultiLine 0x00020001 Can be combined with text and its variations to allow multiple lines of text in the field. If this flag is not set, the text field will be constrained to a single line. Corresponds toTYPE_TEXT_FLAG_MULTI_LINE.
textImeMultiLine 0x00040001 Can be combined with text and its variations to indicate that though the regular text view should not be multiple lines, the IME should provide multiple lines if it can. Corresponds toTYPE_TEXT_FLAG_IME_MULTI_LINE.
textNoSuggestions 0x00080001 Can be combined with text and its variations to indicate that the IME should not show any dictionary-based word suggestions. Corresponds to TYPE_TEXT_FLAG_NO_SUGGESTIONS.
textUri 0x00000011 Text that will be used as a URI. Corresponds to TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_URI.
textEmailAddress 0x00000021 Text that will be used as an e-mail address. Corresponds to TYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_EMAIL_ADDRESS.
textEmailSubject 0x00000031 Text that is being supplied as the subject of an e-mail. Corresponds to TYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_EMAIL_SUBJECT.
textShortMessage 0x00000041 Text that is the content of a short message. Corresponds to TYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_SHORT_MESSAGE.
textLongMessage 0x00000051 Text that is the content of a long message. Corresponds to TYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_LONG_MESSAGE.
textPersonName 0x00000061 Text that is the name of a person. Corresponds to TYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_PERSON_NAME.
textPostalAddress 0x00000071 Text that is being supplied as a postal mailing address. Corresponds to TYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_POSTAL_ADDRESS.
textPassword 0x00000081 Text that is a password. Corresponds to TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_PASSWORD.
textVisiblePassword 0x00000091 Text that is a password that should be visible. Corresponds to TYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_VISIBLE_PASSWORD.
textWebEditText 0x000000a1 Text that is being supplied as text in a web form. Corresponds to TYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_WEB_EDIT_TEXT.
textFilter 0x000000b1 Text that is filtering some other data. Corresponds to TYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_FILTER.
textPhonetic 0x000000c1 Text that is for phonetic pronunciation, such as a phonetic name field in a contact entry. Corresponds to TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_PHONETIC.
textWebEmailAddress 0x000000d1 Text that will be used as an e-mail address on a web form. Corresponds to TYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS.
textWebPassword 0x000000e1 Text that will be used as a password on a web form. Corresponds to TYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_WEB_PASSWORD.
number 0x00000002 A numeric only field. Corresponds to TYPE_CLASS_NUMBER | TYPE_NUMBER_VARIATION_NORMAL.
numberSigned 0x00001002 Can be combined with number and its other options to allow a signed number. Corresponds toTYPE_CLASS_NUMBER | TYPE_NUMBER_FLAG_SIGNED.
numberDecimal 0x00002002 Can be combined with number and its other options to allow a decimal (fractional) number. Corresponds to TYPE_CLASS_NUMBER | TYPE_NUMBER_FLAG_DECIMAL.
numberPassword 0x00000012 A numeric password field. Corresponds to TYPE_CLASS_NUMBER |TYPE_NUMBER_VARIATION_PASSWORD.
phone 0x00000003 For entering a phone number. Corresponds to TYPE_CLASS_PHONE.
datetime 0x00000004 For entering a date and time. Corresponds to TYPE_CLASS_DATETIME |TYPE_DATETIME_VARIATION_NORMAL.
date 0x00000014 For entering a date. Corresponds to TYPE_CLASS_DATETIME | TYPE_DATETIME_VARIATION_DATE.
time 0x00000024 For entering a time. Corresponds to TYPE_CLASS_DATETIME | TYPE_DATETIME_VARIATION_TIME.

This corresponds to the global attribute resource symbol inputType.

方式二:改变 android.text.method.TransformationMethod 的值。


实现代码如下:

/**
	 * 显示或隐藏
	 * @param isShow 
	 */
	private void showOrHide(boolean isShow){
		//记住光标开始的位置
		int pos = etPassword.getSelectionStart();
		if(isShow){
			etPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
		}else{
			etPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
			
		}
		etPassword.setSelection(pos);
	}

参见:android.text.method.TransformationMethod

 

android 代码实现密码输入框

android 代码实现密码输入框

些场合,可能需要在运行时令某个 TextView (可能是运行时创建的,也可以是写在 XML 文件中的)。由于无法通过 XML 文件指定其为 password 输入属性,那么如何实现这个效果呢?

TextView 有两个方法:

setInputType(int)
       setTransformationMethod(TransformationMethod)

其中 setInputType 可以更改 TextView 的输入方式:Contact、Email、Date、Time、Short Message、Normal Text、Password 等。还可以指定各种更正选项,如 单词首字母大写、句子首字母大写、自动更正等。

使用方法:

int inputType = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_AUTO_CORRECT
                      | InputType.TYPE_TEXT_FLAG_MULTI_LINE | InputType.TYPE_TEXT_FLAG_AUTO_CORRECT
                      | InputType.TYPE_TEXT_VARIATION_SHORT_MESSAGE;
       textView.setInputType(inputType);

而 setTransformationMethod 则可以支持将输入的字符转换,包括清除换行符、转换为掩码。使用方法:

textView.setTransformationMethod(PasswordTransformationMethod.getInstance());

综合来说,如果需要实现自己的转换,可以通过实现 TransformationMethod 接口来达到你的目的(比如让输入的所有字符都变成 a,或者输入 a 显示 z,输入 z 显示 a 等)。

如需要动态显示隐藏,则可以使用:

 

setTransformationMethod(HideReturnsTransformationMethod.getInstance());
(感谢 前辈 http://my.oschina.net/wpfjiayou1)

Android 仿支付宝密码输入框效果

Android 仿支付宝密码输入框效果

模仿支付宝输入效果,实现很简单,就是画个矩形框和圆形,其他的通过组合view来实现所有功能,虽然简单但是封装起来,方便以后使用,也分享一下,希望对别人也有点帮助。

这里写图片描述

 

1、如何使用,可以设置自己的进入退出动画,不设置则没有动画效果,自己觉得封装之后还是非常用好的。

private MyInputPwdUtil myInputPwdUtil;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myInputPwdUtil = new MyInputPwdUtil(this);
myInputPwdUtil.getMyInputDialogBuilder().setAnimstyle(R.style.dialog_anim);//可以定制自己进入退出动画,不设置没有动画
myInputPwdUtil.setListener(new InputPwdView.InputPwdListener() {
@Override
public void hide() {
myInputPwdUtil.hide();
}
@Override
public void forgetPwd() {
Toast.makeText(MainActivity.this,"忘记密码",Toast.LENGTH_SHORT).show();
}
@Override
public void finishPwd(String pwd) {
Toast.makeText(MainActivity.this,pwd,Toast.LENGTH_SHORT).show();
}
});
}
public void show(View view){
myInputPwdUtil.show();
}

2、输入框实现主要代码,就是绘制矩形和中间的圆形而已。

int height = getHeight();
int width = getWidth();
//画边框
RectF rect = new RectF(0,width,height);
borderPaint.setColor(borderColor);
canvas.drawRoundRect(rect,borderRadius,borderPaint);
//画内容区域
RectF rectContent = new RectF(rect.left + defaultContentMargin,rect.top + defaultContentMargin,rect.right - defaultContentMargin,rect.bottom - defaultContentMargin);
borderPaint.setColor(getResources().getColor(R.color.myInputPwdBase_gray));
canvas.drawRoundRect(rectContent,borderPaint);
//画分割线:分割线数量比密码数少1
borderPaint.setColor(borderColor);
borderPaint.setstrokeWidth(defaultSplitlinewidth);
for (int i = 1; i < passwordLength; i++) {
float x = width * i / passwordLength;
canvas.drawLine(x,x,height,borderPaint);
}
//画密码内容
float px,py = height / 2;
float halfWidth = width / passwordLength / 2;
for (int i = 0; i < textLength; i++) {
px = width * i / passwordLength + halfWidth;
canvas.drawCircle(px,py,passwordWidth,passwordPaint);
}

3、作为library的module,在定义使用到的属性的时候最好特别能区分开,设置特定的开头,这样能避免引入自己的工程之后导致冲突。

以上所述是小编给大家介绍的Android 仿支付宝密码输入框效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

Android 实现仿支付宝的密码均分输入框

Android 实现仿支付宝的密码均分输入框

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

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

关于Android仿微信/支付宝密码输入框的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于Android 之 EditText 为密码输入框时,密码的显示与隐藏、android 代码实现密码输入框、Android 仿支付宝密码输入框效果、Android 实现仿支付宝的密码均分输入框等相关内容,可以在本站寻找。

本文标签: