zoukankan      html  css  js  c++  java
  • EditTextUtil 监听输入字数

    package com.toge.ta.utils;

    import android.text.Editable;
    import android.text.Selection;
    import android.text.TextWatcher;
    import android.widget.EditText;

    /**
    * Created by Administrator on 2015/10/21.
    */
    public class EditTextUtil {
    /*
      * 监听输入内容是否超出最大长度,并设置光标位置
      * */
    public static class MaxLengthWatcher implements TextWatcher {

    private int maxLen = 0;
    private EditText editText = null;

    public MaxLengthWatcher(int maxLen, EditText editText) {
    this.maxLen = maxLen;
    this.editText = editText;
    }

    public void afterTextChanged(Editable arg0) {


    // Selection.setSelection(arg0, arg0.toString().length());
    // TODO Auto-generated method stub
    }

    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
    int arg3) {
    // TODO Auto-generated method stub

    }

    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
    // TODO Auto-generated method stub
    Editable editable = editText.getText();
    String s = editable + "";
    int len = editable.length();

    len = StringUtil.stringLength(s);

    if (len > maxLen) {
    int selEndIndex = Selection.getSelectionEnd(editable);
    String str = editable.toString();
    //截取新字符串
    String newStr = StringUtil.cutString(str, maxLen);
    editText.setText(newStr);
    editable = editText.getText();
    //新字符串的长度
    int newLen = editable.length();
    //旧光标位置超过字符串长度
    if (selEndIndex > newLen) {
    selEndIndex = editable.length();
    }
    //设置新光标所在的位置
    Selection.setSelection(editable, selEndIndex);
    }
    }
    }
    }
  • 相关阅读:
    phpstorm+xdebug配置
    php5.4 traits
    psr-4
    oAuth 认证和授权原理
    跨域解决方案
    【微信公众平台开发】利用百度接口,制作一键导航功能
    php 加密压缩
    jquery validate使用笔记
    where和having
    在join中,on和where的区别
  • 原文地址:https://www.cnblogs.com/aikongmeng/p/5181274.html
Copyright © 2011-2022 走看看