@Override
  public void afterTextChanged(Editable s) {
    String mask = mMask;
    String value = s.toString();
    if (value.equals(mResult)) return;
    try {
        /* prepare the formatter*/
      MaskedFormatter formatter = new MaskedFormatter(mask);
      formatter.setValueContainsLiteralCharacters(false);
      formatter.setPlaceholderCharacter(
          (char) 1); /* get a string with applied mask and placeholder chars*/
      value = formatter.valueToString(value);
      try {
          /* find first placeholder*/
        value = value.substring(0, value.indexOf((char) 1)); /*process a mask char*/
        if (value.charAt(value.length() - 1) == mask.charAt(value.length() - 1))
          value = value.substring(0, value.length() - 1);

      } catch (Exception e) {
      }

      mResult = value;

      s.replace(0, s.length(), value);

    } catch (ParseException e) {

      // the entered value does not match a mask
      int offset = e.getErrorOffset();
      value = removeCharAt(value, offset);
      s.replace(0, s.length(), value);
    }
  }
  public static void format(Editable text) {
    // Here, "root" means the position of "'":
    // 0'3, 0'90, and +81'-90
    // (dash will be deleted soon, so it is actually +81'90).
    int rootIndex = 1;
    int length = text.length();
    if (length > 3 && text.subSequence(0, 3).toString().equals("+81")) {
      rootIndex = 3;
    } else if (length < 1 || text.charAt(0) != '0') {
      return;
    }

    CharSequence saved = text.subSequence(0, length);

    // Strip the dashes first, as we're going to add them back
    int i = 0;
    while (i < text.length()) {
      if (text.charAt(i) == '-') {
        text.delete(i, i + 1);
      } else {
        i++;
      }
    }

    length = text.length();
    int dashposition;

    i = rootIndex;
    int base = 0;
    while (i < length) {
      char ch = text.charAt(i);
      if (!Character.isDigit(ch)) {
        text.replace(0, length, saved);
        return;
      }
      short value = FORMAT_MAP[base + ch - '0'];
      if (value < 0) {
        if (value <= -100) {
          text.replace(0, length, saved);
          return;
        }
        int dashPos2 = rootIndex + (Math.abs(value) % 10);
        if (length > dashPos2) {
          text.insert(dashPos2, "-");
        }
        int dashPos1 = rootIndex + (Math.abs(value) / 10);
        if (length > dashPos1) {
          text.insert(dashPos1, "-");
        }
        break;
      } else {
        base = value;
        i++;
      }
    }

    if (length > 3 && rootIndex == 3) {
      text.insert(rootIndex, "-");
    }
  }
  /** @see BaseInputConnection#setComposingRegion(int, int) */
  @Override
  public boolean setComposingRegion(int start, int end) {
    if (DEBUG) Log.w(TAG, "setComposingRegion [" + start + " " + end + "]");
    int textLength = mEditable.length();
    int a = Math.min(start, end);
    int b = Math.max(start, end);
    if (a < 0) a = 0;
    if (b < 0) b = 0;
    if (a > textLength) a = textLength;
    if (b > textLength) b = textLength;

    CharSequence regionText = null;
    if (a == b) {
      removeComposingSpans(mEditable);
    } else {
      if (a == 0 && b == mEditable.length()) {
        regionText = mEditable.subSequence(a, b);
        // If setting composing region that matches, at least in length, of the entire
        // editable region then check it for image placeholders.  If any are found,
        // don't continue this operation.
        // This fixes the problem where, on Android 4.3, pasting an image is followed
        // by setting the composing region which then causes the image to be deleted.
        // http://crbug.com/466755
        for (int i = a; i < b; ++i) {
          if (regionText.charAt(i) == '\uFFFC') return true;
        }
      }
      super.setComposingRegion(a, b);
    }
    updateSelectionIfRequired();

    return mImeAdapter.setComposingRegion(regionText, a, b);
  }
Ejemplo n.º 4
0
 @Override
 public void onItemClick(int facesPos, int viewIndex) {
   int deleteId = (++viewIndex) * (SysConstant.pageSize - 1);
   if (deleteId > Emoparser.getInstance(MessageActivity.this).getResIdList().length) {
     deleteId = Emoparser.getInstance(MessageActivity.this).getResIdList().length;
   }
   if (deleteId == facesPos) {
     String msgContent = messageEdt.getText().toString();
     if (msgContent.isEmpty()) return;
     if (msgContent.contains("["))
       msgContent = msgContent.substring(0, msgContent.lastIndexOf("["));
     messageEdt.setText(msgContent);
   } else {
     int resId = Emoparser.getInstance(MessageActivity.this).getResIdList()[facesPos];
     String pharse = Emoparser.getInstance(MessageActivity.this).getIdPhraseMap().get(resId);
     int startIndex = messageEdt.getSelectionStart();
     Editable edit = messageEdt.getEditableText();
     if (startIndex < 0 || startIndex >= edit.length()) {
       if (null != pharse) {
         edit.append(pharse);
       }
     } else {
       if (null != pharse) {
         edit.insert(startIndex, pharse);
       }
     }
   }
   Editable edtable = messageEdt.getText();
   int position = edtable.length();
   Selection.setSelection(edtable, position);
 }
Ejemplo n.º 5
0
  public void onTextChanged(CharSequence ss, int start, int before, int count) {
    editText.setTextColor(Color.BLACK);

    Editable editable = editText.getText();
    int len = editable.length();
    boolean bOK = false;

    if (checkLength(len)) {
      if (checkData(editable.toString())) bOK = true;
    }
    if (!bOK) {
      int selEndIndex = Selection.getSelectionEnd(editable);
      String str = editable.toString();
      String newStr = str.substring(0, len - 1);
      editText.setText(newStr);
      editable = editText.getText();
      // 新字符串长度
      int newLen = editable.length();
      // 旧光标位置超过字符串长度
      if (selEndIndex > newLen) {
        selEndIndex = newLen;
      }
      // 设置新的光标所在位置
      Selection.setSelection(editable, selEndIndex);

    } else {
      saveData(editable.toString());
    }
  }
Ejemplo n.º 6
0
  private void processImage(boolean opening, Editable output) {
    Object[] spans = output.getSpans(0, output.length(), ImageSpan.class);
    if (spans.length == 0) {
      return;
    }

    ImageSpan imageSpan = (ImageSpan) spans[0];
    String imageUrl = imageSpan.getSource();

    // Handle image video thumbnail clicked
    if (imageUrl.startsWith(BBCodeParser.VIDEO_URL_PREFIX)) {
      VideoClickableSpan clickableSpan = mVideoClickableSpanProvider.get();
      clickableSpan.setVideoUrl(imageUrl.substring(BBCodeParser.VIDEO_URL_PREFIX.length()));
      output.setSpan(
          clickableSpan,
          output.getSpanStart(imageSpan),
          output.length(),
          Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
      return;
    }

    // Not Image Video Thumbnail, This is normal image
    if (TextUtils.isEmpty(imageUrl) || !RichTextUtils.isUrl(imageUrl)) {
      return;
    }

    ImageClickableSpan clickableSpan = mImageClickableSpanProvider.get();
    clickableSpan.setImageUrl(imageUrl);
    output.setSpan(
        clickableSpan,
        output.getSpanStart(imageSpan),
        output.length(),
        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  }
Ejemplo n.º 7
0
  /**
   * Replace {@link android.view.View.OnClickListener} with {@link
   * cl.monsoon.s1next.widget.TagHandler.ImageClickableSpan}.
   *
   * <p>See android.text.HtmlToSpannedConverter#startImg(android.text.SpannableStringBuilder,
   * org.xml.sax.Attributes, android.text.Html.ImageGetter)
   */
  private void handleImg(boolean opening, Editable output) {
    if (!opening) {
      int end = output.length();

      // \uFFFC: OBJECT REPLACEMENT CHARACTER
      int len = "\uFFFC".length();
      ImageSpan imageSpan = output.getSpans(end - len, end, ImageSpan.class)[0];

      String url = imageSpan.getSource();
      // replace \uFFFC with ImageSpan's source
      // in order to support url copy when selected
      output.replace(end - len, end, url);

      // image from server doesn't have domain
      // skip this because we don't want to
      // make this image (emoticon or something
      // others) clickable
      if (URLUtil.isNetworkUrl(url)) {

        output.removeSpan(imageSpan);
        // make this ImageSpan clickable
        output.setSpan(
            new ImageClickableSpan(mContext, imageSpan.getDrawable(), url),
            end - len,
            output.length(),
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
      }
    }
  }
Ejemplo n.º 8
0
 /**
  * 光标移动到最后
  *
  * @param editText
  */
 private void moveCursor(EditText editText) {
   Editable etext = editText.getText();
   if (etext.length() < 1) {
   } else {
     Selection.setSelection(etext, etext.length());
   }
 }
Ejemplo n.º 9
0
 private void testing() {
   if (emailE.length() == 0 && passwordE.length() == 0) {
     textView.setVisibility(View.VISIBLE);
     textView.setTextColor(getResources().getColor(R.color.Error));
     textView.setText(R.string.EmailAndPassNull);
   } else if (emailE.length() == 0) {
     textView.setVisibility(View.VISIBLE);
     textView.setTextColor(getResources().getColor(R.color.Error));
     textView.setText(R.string.EmailNull);
   } else if (passwordE.length() == 0) {
     textView.setVisibility(View.VISIBLE);
     textView.setTextColor(getResources().getColor(R.color.Error));
     textView.setText(R.string.PassNull);
   } else if (emailTest(emailE)) {
     textView.setVisibility(View.VISIBLE);
     textView.setTextColor(getResources().getColor(R.color.Ok));
     textView.setText("Ok");
     User.login = emailE.toString();
     User.password = passwordE.toString();
     HTTPClient.login(this);
   } else {
     textView.setVisibility(View.VISIBLE);
     textView.setTextColor(getResources().getColor(R.color.Error));
     textView.setText("Неверный Email");
   }
 }
Ejemplo n.º 10
0
        public void handleTag(
            final boolean opening,
            final String tag,
            final Editable output,
            final XMLReader xmlReader) {
          if (TAG_DEL.equalsIgnoreCase(tag)) {
            if (opening) startSpan(new StrikethroughSpan(), output);
            else endSpan(StrikethroughSpan.class, output);
            return;
          }

          if (TAG_UL.equalsIgnoreCase(tag) || TAG_OL.equalsIgnoreCase(tag)) {
            if (opening) {
              listElements.add(new ListSeparator(TAG_OL.equalsIgnoreCase(tag)));
            } else if (!listElements.isEmpty()) {
              listElements.removeLast();
            }

            if (!opening && listElements.isEmpty()) output.append('\n');
            return;
          }

          if (TAG_LI.equalsIgnoreCase(tag) && opening && !listElements.isEmpty()) {
            listElements.getLast().append(output, listElements.size());
            return;
          }

          if (TAG_CODE.equalsIgnoreCase(tag)) {
            if (opening) startSpan(new TypefaceSpan("monospace"), output);
            else endSpan(TypefaceSpan.class, output);
          }

          if (TAG_PRE.equalsIgnoreCase(tag)) {
            output.append('\n');
            if (opening) startSpan(new TypefaceSpan("monospace"), output);
            else endSpan(TypefaceSpan.class, output);
          }

          if ((TAG_ROOT.equalsIgnoreCase(tag) || TAG_HTML.equalsIgnoreCase(tag)) && !opening) {
            // Remove leading newlines
            while (output.length() > 0 && output.charAt(0) == '\n') output.delete(0, 1);

            // Remove trailing newlines
            int last = output.length() - 1;
            while (last >= 0 && output.charAt(last) == '\n') {
              output.delete(last, last + 1);
              last = output.length() - 1;
            }

            QuoteSpan[] quoteSpans = output.getSpans(0, output.length(), QuoteSpan.class);
            for (QuoteSpan span : quoteSpans) {
              int start = output.getSpanStart(span);
              int end = output.getSpanEnd(span);
              output.removeSpan(span);
              output.setSpan(new ReplySpan(), start, end, SPAN_EXCLUSIVE_EXCLUSIVE);
            }
          }
        }
  private void updateHint() {
    Editable text = getText();
    CharSequence hintText = getHint();
    if (text == null || hintText == null) {
      return;
    }

    // Show hint if we need to
    if (prefix.length() > 0) {
      HintSpan[] hints = text.getSpans(0, text.length(), HintSpan.class);
      HintSpan hint = null;
      int testLength = prefix.length();
      if (hints.length > 0) {
        hint = hints[0];
        testLength += text.getSpanEnd(hint) - text.getSpanStart(hint);
      }

      if (text.length() == testLength) {
        hintVisible = true;

        if (hint != null) {
          return; // hint already visible
        }

        // We need to display the hint manually
        Typeface tf = getTypeface();
        int style = Typeface.NORMAL;
        if (tf != null) {
          style = tf.getStyle();
        }
        ColorStateList colors = getHintTextColors();

        HintSpan hintSpan = new HintSpan(null, style, (int) getTextSize(), colors, colors);
        text.insert(prefix.length(), hintText);
        text.setSpan(
            hintSpan,
            prefix.length(),
            prefix.length() + getHint().length(),
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        setSelection(prefix.length());

      } else {
        if (hint == null) {
          return; // hint already removed
        }

        // Remove the hint. There should only ever be one
        int sStart = text.getSpanStart(hint);
        int sEnd = text.getSpanEnd(hint);

        text.removeSpan(hint);
        text.replace(sStart, sEnd, "");

        hintVisible = false;
      }
    }
  }
Ejemplo n.º 12
0
 /**
  * 移除字符</br>
  *
  * @param c
  */
 private void removeChar(char c) {
   Editable editable = mEditText.getText();
   if (editable.length() <= 0) {
     return;
   }
   if (editable.charAt(editable.length() - 1) == c) {
     editable.delete(editable.length() - 1, editable.length());
   }
 }
 @Override
 public void onClick(View v) {
   int id = v.getId();
   if (id == R.id.bold) {
     AnalyticsTracker.track(Stat.EDITOR_TAPPED_BOLD);
     onFormatButtonClick(mBoldToggleButton, TAG_FORMAT_BAR_BUTTON_STRONG);
   } else if (id == R.id.em) {
     AnalyticsTracker.track(Stat.EDITOR_TAPPED_ITALIC);
     onFormatButtonClick(mEmToggleButton, TAG_FORMAT_BAR_BUTTON_EM);
   } else if (id == R.id.underline) {
     AnalyticsTracker.track(Stat.EDITOR_TAPPED_UNDERLINE);
     onFormatButtonClick(mUnderlineToggleButton, TAG_FORMAT_BAR_BUTTON_UNDERLINE);
   } else if (id == R.id.strike) {
     AnalyticsTracker.track(Stat.EDITOR_TAPPED_STRIKETHROUGH);
     onFormatButtonClick(mStrikeToggleButton, TAG_FORMAT_BAR_BUTTON_STRIKE);
   } else if (id == R.id.bquote) {
     AnalyticsTracker.track(Stat.EDITOR_TAPPED_BLOCKQUOTE);
     onFormatButtonClick(mBquoteToggleButton, TAG_FORMAT_BAR_BUTTON_QUOTE);
   } else if (id == R.id.more) {
     AnalyticsTracker.track(Stat.EDITOR_TAPPED_MORE);
     mSelectionEnd = mContentEditText.getSelectionEnd();
     Editable str = mContentEditText.getText();
     if (str != null) {
       if (mSelectionEnd > str.length()) mSelectionEnd = str.length();
       str.insert(mSelectionEnd, "\n<!--more-->\n");
     }
   } else if (id == R.id.link) {
     AnalyticsTracker.track(Stat.EDITOR_TAPPED_LINK);
     mSelectionStart = mContentEditText.getSelectionStart();
     mStyleStart = mSelectionStart;
     mSelectionEnd = mContentEditText.getSelectionEnd();
     if (mSelectionStart > mSelectionEnd) {
       int temp = mSelectionEnd;
       mSelectionEnd = mSelectionStart;
       mSelectionStart = temp;
     }
     Intent i = new Intent(getActivity(), EditLinkActivity.class);
     if (mSelectionEnd > mSelectionStart) {
       if (mContentEditText.getText() != null) {
         String selectedText =
             mContentEditText
                 .getText()
                 .subSequence(mSelectionStart, mSelectionEnd)
                 .toString();
         i.putExtra("selectedText", selectedText);
       }
     }
     startActivityForResult(i, ACTIVITY_REQUEST_CODE_CREATE_LINK);
   } else if (id == R.id.addPictureButton) {
     AnalyticsTracker.track(Stat.EDITOR_TAPPED_IMAGE);
     mEditorFragmentListener.onAddMediaClicked();
     if (isAdded()) {
       getActivity().openContextMenu(mAddPictureButton);
     }
   }
 }
Ejemplo n.º 14
0
 private int findLineEnd(Editable text, int current) {
   if (DBG) {
     Log.d(LOG_TAG, "--- findLineEnd: curr:" + current + ", length:" + text.length());
   }
   int pos = current;
   for (; pos < text.length(); pos++) {
     if (pos > 0 && text.charAt(pos - 1) == '\n') {
       break;
     }
   }
   return pos;
 }
Ejemplo n.º 15
0
  public void insertMention(int uid) {
    UserVM user = users().get(uid);
    String name = user.getName().get();
    String nick = user.getNick().get();
    Editable text = messageEditText.getText();
    if (text.length() > 0 && text.charAt(text.length() - 1) != ' ') text.append(" ");

    String mentionString = ((nick != null && !nick.isEmpty()) ? "@" + nick : name) + ": ";

    text.append(mentionString);
    messageEditText.requestFocus();
    keyboardUtils.setImeVisibility(messageEditText, true);
  }
Ejemplo n.º 16
0
 private void sanitizeBetween() {
   // Find the last chip.
   MemberChip[] recips = getSortedRecipients();
   if (recips != null && recips.length > 0) {
     MemberChip last = recips[recips.length - 1];
     MemberChip beforeLast = null;
     if (recips.length > 1) {
       beforeLast = recips[recips.length - 2];
     }
     int startLooking = 0;
     int end = getSpannable().getSpanStart(last);
     if (beforeLast != null) {
       startLooking = getSpannable().getSpanEnd(beforeLast);
       Editable text = getText();
       if (startLooking == -1 || startLooking > text.length() - 1) {
         // There is nothing after this chip.
         return;
       }
       if (text.charAt(startLooking) == ' ') {
         startLooking++;
       }
     }
     if (startLooking >= 0 && end >= 0 && startLooking < end) {
       getText().delete(startLooking, end);
     }
   }
 }
Ejemplo n.º 17
0
 @Override
 public void afterTextChanged(Editable s) {
   count = 140 - s.length();
   if (count < 0) countTV.setTextColor(Color.RED);
   else countTV.setTextColor(Color.BLACK);
   countTV.setText("" + count);
 }
Ejemplo n.º 18
0
 /** 表情点击事件 */
 @Override
 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
   int index = replyContent.getSelectionStart();
   Editable e = replyContent.getEditableText();
   if (index < 0 || index > e.length()) e.append(FaceUtil.getFaceList().get(position));
   else e.insert(index, FaceUtil.getFaceList().get(position));
 }
Ejemplo n.º 19
0
 private static void endSpan(Class<?> type, Editable output) {
   int length = output.length();
   Object span = getLast(output, type);
   int start = output.getSpanStart(span);
   output.removeSpan(span);
   if (start != length) output.setSpan(span, start, length, SPAN_EXCLUSIVE_EXCLUSIVE);
 }
Ejemplo n.º 20
0
 @Override
 public void onKey(int primaryCode, int[] keyCodes) {
   Editable editable = ed.getText();
   int start = ed.getSelectionStart();
   if (primaryCode == Keyboard.KEYCODE_CANCEL) { // 完成
     hideKeyboard();
   } else if (primaryCode == Keyboard.KEYCODE_DELETE) { // 回退
     if (editable != null && editable.length() > 0) {
       if (start > 0) {
         editable.delete(start - 1, start);
       }
     }
   } else if (primaryCode == Keyboard.KEYCODE_SHIFT) { // 大小写切换
     changeKey();
     keyboardView.setKeyboard(k1);
   } else if (primaryCode == Keyboard.KEYCODE_MODE_CHANGE) { // 数字键盘切换
     if (isnun) {
       isnun = false;
       keyboardView.setKeyboard(k1);
     } else {
       isnun = true;
       keyboardView.setKeyboard(k2);
     }
   } else if (primaryCode == 57419) { // go left
     if (start > 0) {
       ed.setSelection(start - 1);
     }
   } else if (primaryCode == 57421) { // go right
     if (start < ed.length()) {
       ed.setSelection(start + 1);
     }
   } else {
     editable.insert(start, Character.toString((char) primaryCode));
   }
 }
Ejemplo n.º 21
0
 @Override
 public void afterTextChanged(Editable s) {
   length = s.length();
   if (length == 0) {
     clearInput.setVisibility(GONE);
   }
 }
Ejemplo n.º 22
0
 // TextWatcher methods
 public void afterTextChanged(Editable statusText) {
   int count = 140 - statusText.length();
   textCount.setText(Integer.toString(count));
   textCount.setTextColor(Color.GREEN);
   if (count < 10) textCount.setTextColor(Color.YELLOW);
   if (count < 0) textCount.setTextColor(Color.RED);
 }
 public void afterTextChanged(Editable text) {
   if (text.length() == 0) {
     mNameView.setError(getContext().getText(R.string.errorEmptyName));
   } else {
     mNameView.setError(null);
   }
 }
Ejemplo n.º 24
0
 public static void insertSeparators(Editable s) {
   final int[] positions = {4, 9, 14};
   for (int i : positions) {
     if (s.length() > i) {
       s.insert(i, SEPARATOR);
     }
   }
 }
Ejemplo n.º 25
0
 private void cutContent() {
   final Editable text = getText();
   int textLength = text.length();
   setSelection(0, textLength);
   setPrimaryClip(ClipData.newPlainText(null, text));
   ((Editable) getText()).delete(0, textLength);
   setSelection(0);
 }
Ejemplo n.º 26
0
 // 在文字改变后调用
 @Override
 public void afterTextChanged(Editable s) {
   if (s.length() == 0) {
     hideBtn(); // 隐藏按钮
   } else {
     showBtn(); // 显示按钮
   }
 }
Ejemplo n.º 27
0
 /**
  * Add a chip to this view
  *
  * @param chip The chip to add
  */
 public void addChip(Chip chip) {
   Editable text = getText();
   Spannable span = getChipSpan(chip);
   text.append(span);
   setText(text);
   setSelection(text.length());
   onChipAdded(chip);
 }
Ejemplo n.º 28
0
 public static char[] getPassword(Editable s) {
   int length = s.length();
   char[] password = new char[length];
   for (int i = 0; i < length; i++) {
     password[i] = s.charAt(i);
   }
   return password;
 }
 @Override
 public void afterTextChanged(Editable s) {
   // TODO Auto-generated method stub
   if (s.length() > 0) {
     mConfirmNumberButton.setEnabled(true);
   } else {
     mConfirmNumberButton.setEnabled(false);
   }
 }
 @Override
 public void afterTextChanged(Editable editable) {
   if (onResponseChangedListener != null
       && (lastString != null && lastString.length() > 0)) {
     String response = editable.length() > 0 ? editable.toString() : null;
     onResponseChangedListener.onResponseChanged(rule, response);
   }
   lastString = editable.toString();
 }