@Override
  public CharSequence filter(
      CharSequence src, int start, int end, Spanned dest, int dstart, int dend) {
    int dindex = 0;
    int count = 0;
    int attempt = 0;

    maxLen = maxlenChar;
    while (dindex < src.length()) {
      char c = src.charAt(dindex++);
      count++;
      if (c >= 128) {
        maxLen = maxLenHanzi;
      }
    }
    dindex = 0;
    while (dindex < dest.length()) {
      char c = dest.charAt(dindex++);
      count++;
      if (c >= 128) {
        maxLen = maxLenHanzi;
      }
    }

    attempt = count;
    if (count > maxLen) {
      // more words input than you want ,give up the redundant character
      count = maxLen;
      src = "";
    }
    if (mCallBack != null) {
      mCallBack.lengthChange(count, attempt);
    }
    return src;
  }
Exemple #2
0
 @Override
 public CharSequence filter(
     CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
   if ((end == 1)
       && (dstart != 0)
       && (dend != dest.length())
       && (source.charAt(start) == '\n')) {
     StringBuilder ident = new StringBuilder();
     // find the last line
     int n = 1;
     while (dest.charAt(dstart - n) != '\n') ++n;
     // copy previous ident and autoexpand comments
     int s = 1;
     char c = dest.charAt(dstart - n + s++);
     while (c != '\n' && (c == ' ' || c == '/')) {
       ident.append(c);
       c = dest.charAt(dstart - n + s++);
     }
     // extra ident after these chars
     if ("{(*+/-=%".contains(String.valueOf(dest.charAt(dstart - 1)))) ident.append("   ");
     return source + ident.toString();
   }
   return source;
 }
  private void findChars(@NonNull Spanned s, int start, int end, int offset, int[] out) {
    for (int i = start; i < end; i++) {
      final char c = s.charAt(i);

      if (isSignChar(c)) {
        if (out[CHAR_SIGN] == -1 && out[CHAR_EXP] == -1) {
          // count in only signs before exponent
          out[CHAR_SIGN] = i + offset;
        }
      } else if (isDecimalPointChar(c)) {
        if (out[CHAR_POINT] == -1) {
          out[CHAR_POINT] = i + offset;
        }
      } else if (isExponentChar(c)) {
        if (out[CHAR_EXP] == -1) {
          out[CHAR_EXP] = i + offset;
        }
      }
    }
  }
  @Override
  public CharSequence filter(
      CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
    Log.i(
        "test_filter",
        "source = "
            + source
            + ", start = "
            + start
            + ", end = "
            + end
            + ", dest = "
            + dest
            + ", dstart = "
            + dstart
            + ", dend = "
            + dend);
    int crtLength = dest.length();
    String numStr = getIntPart(source);
    if (crtLength == 0 || dend - dstart == crtLength)
      return numStr.substring(0, max > numStr.length() ? numStr.length() : max);

    int returnLength = max - dest.length() + dend - dstart;
    if (numStr.length() < returnLength) returnLength = numStr.length();
    if (dest.charAt(0) == '0') {
      if (dstart > 0) {
        text.setText(numStr.substring(0, returnLength));
        text.setSelection(returnLength);
        return "";
      }
      if (numStr.startsWith("0")) return "";
      return numStr.substring(0, returnLength);
    }
    if (numStr.startsWith("0")) {
      if (dstart == 0) return "";
      return numStr.substring(0, returnLength);
    }
    return numStr.substring(0, returnLength);
  }