@Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
      Editable text = getText();
      if (text == null) return;

      clearSelections();
      updateHint();

      TokenImageSpan[] spans =
          text.getSpans(start - before, start - before + count, TokenImageSpan.class);

      for (TokenImageSpan token : spans) {

        int position = start + count;
        if (text.getSpanStart(token) < position && position <= text.getSpanEnd(token)) {
          // We may have to manually reverse the auto-complete and remove the extra ,'s
          int spanStart = text.getSpanStart(token);
          int spanEnd = text.getSpanEnd(token);

          removeToken(token, text);

          // The end of the span is the character index after it
          spanEnd--;

          if (spanEnd >= 0 && text.charAt(spanEnd) == ',') {
            text.delete(spanEnd, spanEnd + 1);
          }

          if (spanStart >= 0 && text.charAt(spanStart) == ',') {
            text.delete(spanStart, spanStart + 1);
          }
        }
      }
    }
Example #2
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 createLinkFromSelection(String linkURL, String linkText) {
   try {
     if (linkURL != null && !linkURL.equals("http://") && !linkURL.equals("")) {
       if (mSelectionStart > mSelectionEnd) {
         int temp = mSelectionEnd;
         mSelectionEnd = mSelectionStart;
         mSelectionStart = temp;
       }
       Editable editable = mContentEditText.getText();
       if (editable == null) {
         return;
       }
       if (mIsLocalDraft) {
         if (linkText == null) {
           if (mSelectionStart < mSelectionEnd) {
             editable.delete(mSelectionStart, mSelectionEnd);
           }
           editable.insert(mSelectionStart, linkURL);
           editable.setSpan(
               new URLSpan(linkURL),
               mSelectionStart,
               mSelectionStart + linkURL.length(),
               Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
           mContentEditText.setSelection(mSelectionStart + linkURL.length());
         } else {
           if (mSelectionStart < mSelectionEnd) {
             editable.delete(mSelectionStart, mSelectionEnd);
           }
           editable.insert(mSelectionStart, linkText);
           editable.setSpan(
               new URLSpan(linkURL),
               mSelectionStart,
               mSelectionStart + linkText.length(),
               Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
           mContentEditText.setSelection(mSelectionStart + linkText.length());
         }
       } else {
         if (linkText == null) {
           if (mSelectionStart < mSelectionEnd) {
             editable.delete(mSelectionStart, mSelectionEnd);
           }
           String urlHTML = "<a href=\"" + linkURL + "\">" + linkURL + "</a>";
           editable.insert(mSelectionStart, urlHTML);
           mContentEditText.setSelection(mSelectionStart + urlHTML.length());
         } else {
           if (mSelectionStart < mSelectionEnd) {
             editable.delete(mSelectionStart, mSelectionEnd);
           }
           String urlHTML = "<a href=\"" + linkURL + "\">" + linkText + "</a>";
           editable.insert(mSelectionStart, urlHTML);
           mContentEditText.setSelection(mSelectionStart + urlHTML.length());
         }
       }
     }
   } catch (RuntimeException e) {
     AppLog.e(T.POSTS, e);
   }
 }
 public void afterTextChanged(Editable edt) {
   String temp = edt.toString();
   if (temp.length() == 2) {
     int mid = temp.substring(0, 1).toCharArray()[0];
     int mid1 = temp.substring(1, 2).toCharArray()[0];
     if (mid > 50) {
       edt.delete(1, 2);
     } else if (mid == 50 && mid1 > 51) {
       edt.delete(1, 2);
     }
   }
 }
Example #5
0
 public void afterTextChanged(Editable s) {
   if (mMsgContent.length() > msgContentCount) {
     // VoiceApplication.getInstance().showToast(R.string.toast_declared_word_number);
     s.delete(msgContentCount, mMsgContent.length());
   }
   updateEditCounter();
 }
Example #6
0
 @Override
 public void afterTextChanged(Editable edt) {
   if (filterFlag > 0) {
     try {
       String temp = edt.toString();
       String tem = temp.substring(temp.length() - 1, temp.length());
       char[] temC = tem.toCharArray();
       int mid = temC[0];
       if (filterFlag == 1 || filterFlag == 2) {
         if (mid >= 48 && mid <= 57) { // 数字
           return;
         }
         if (mid >= 65 && mid <= 90) { // 大写字母 和@
           return;
         }
         if (mid >= 97 && mid <= 122) { // 小写字母
           return;
         }
         if (mid == 46) { // .
           return;
         }
         if (mid == 64) { // @
           return;
         }
       } else {
         if (mid >= 33 && mid <= 126) {
           return;
         }
       }
       edt.delete(temp.length() - 1, temp.length());
     } catch (Exception e) {
     }
   }
 }
 @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));
   }
 }
  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, "-");
    }
  }
 public static void removeSeparators(Editable s) {
   int index = TextUtils.indexOf(s, SEPARATOR);
   while (index >= 0) {
     s.delete(index, index + 1);
     index = TextUtils.indexOf(s, SEPARATOR, index + 1);
   }
 }
Example #10
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 onTextChanged(CharSequence s, int start, int before, int count) {
    if (mIsUpdating) {
      mIsUpdating = false;
      return;
    }

    Editable editable = mEditText.getText();
    String string = s.toString();
    int end = count + start;
    String inserted = string.substring(start, end);

    if (mAcceptOnlyNumbers && !inserted.matches("[0-9]*") && !string.equalsIgnoreCase("")) {
      mIsUpdating = true;
      editable.delete(start, end);
      // TODO
      // I need a solution to not set text "again". Changes keyboard and selection
      mIsUpdating = true;
      mEditText.setText(mEditText.getText());
      mEditText.setSelection(end - 1);
      return;
    }

    int currentLength = s.length(), maxLength = mMask.length();
    if (currentLength > maxLength) {
      mIsUpdating = true;
      editable.delete(maxLength, currentLength);
      // TODO
      // I need a solution to not set text "again". Changes keyboard and selection
      mIsUpdating = true;
      mEditText.setText(mEditText.getText());
      mEditText.setSelection(maxLength);
      return;
    }

    for (int i = start; i < editable.length(); i++) {
      char c = mMask.charAt(i);
      char editableC = editable.charAt(i);
      if (c != '#' && c != editableC) {
        mIsUpdating = true;
        editable.insert(i, String.valueOf(c));
      }
    }
  }
Example #12
0
 @Override
 public void afterTextChanged(Editable s) {
   if (inHexMode()) {
     Matcher m = nonHexPattern.matcher(s);
     while (m.find()) {
       s.delete(m.start(), m.end());
       m.reset(s);
     }
   }
 }
 public void afterTextChanged(Editable edt) {
   String temp = edt.toString();
   for (int i = temp.length(); i > 0; i--) {
     int mid = temp.substring(i - 1, i).toCharArray()[0];
     if ((mid < 48 || mid > 57) && ((mid < 65 || mid > 90) && (mid < 97 || mid > 122))) {
       edt.delete(i - 1, i);
       return;
     }
   }
 }
 @Override
 public void afterTextChanged(Editable s) {
   editStart = mSignatureEt.getSelectionStart();
   editEnd = mSignatureEt.getSelectionEnd();
   if (temp.length() > 0) {
     mTextCountTv.setText("" + (30 - temp.length()) + "");
   }
   if (temp.length() > 30) {
     s.delete(editStart - 1, editEnd);
     int tempSelection = editStart;
     mSignatureEt.setText(s);
     mSignatureEt.setSelection(tempSelection);
   }
 }
        @Override
        public void afterTextChanged(Editable s) {
          Log.v("TextWatcher-afterTextChanged-->", s.toString() + "length" + s.length());
          // 输入的时候,只有一个光标,那么这两个值应该是相等的。。。
          editStart = et_user_ban.getSelectionStart();
          editEnd = et_user_ban.getSelectionEnd();

          // 限定EditText只能输入19个数字,并且达到19个的时候用红色显示
          if (charSequence.length() > 19) {
            Toast.makeText(AddBankCardActivity.this, "你输入的字数已经超过了限制,不能再输入!", Toast.LENGTH_SHORT)
                .show();
            // 默认光标在最前端,所以当输入第19个数字的时候,删掉(光标位置从11-1到11)的数字,这样就无法输入超过19个以后的数字
            s.delete(editStart - 1, editEnd);
            // 当输入超过第19个数字的时候,改变字体颜色为红色
          }
        }
  @Override
  public boolean performContextMenuAction(int id) {
    Editable editable = getEditable();
    if (editable == null) {
      return false;
    }
    int selStart = Selection.getSelectionStart(editable);
    int selEnd = Selection.getSelectionEnd(editable);

    switch (id) {
      case R.id.selectAll:
        setSelection(0, editable.length());
        break;
      case R.id.cut:
        // If selection is empty, we'll select everything
        if (selStart == selEnd) {
          // Fill the clipboard
          Clipboard.setText(editable);
          editable.clear();
        } else {
          Clipboard.setText(
              editable
                  .toString()
                  .substring(Math.min(selStart, selEnd), Math.max(selStart, selEnd)));
          editable.delete(selStart, selEnd);
        }
        break;
      case R.id.paste:
        commitText(Clipboard.getText(), 1);
        break;
      case R.id.copy:
        // Copy the current selection or the empty string if nothing is selected.
        String copiedText =
            selStart == selEnd
                ? ""
                : editable
                    .toString()
                    .substring(Math.min(selStart, selEnd), Math.max(selStart, selEnd));
        Clipboard.setText(copiedText);
        break;
    }
    return true;
  }
  private void removeSpan(TokenImageSpan span) {
    Editable text = getText();
    if (text == null) return;

    // If the spanwatcher has been removed, we need to also manually trigger onSpanRemoved
    TokenSpanWatcher[] spans = text.getSpans(0, text.length(), TokenSpanWatcher.class);
    if (spans.length == 0) {
      spanWatcher.onSpanRemoved(text, span, text.getSpanStart(span), text.getSpanEnd(span));
    } else if (Build.VERSION.SDK_INT < 14) {
      // HACK: Need to manually trigger on Span removed if there is only 1 object
      // not sure if there's a cleaner way
      if (objects.size() == 1) {
        spanWatcher.onSpanRemoved(text, span, text.getSpanStart(span), text.getSpanEnd(span));
      }
    }

    // Add 1 to the end because we put a " " at the end of the spans when adding them
    text.delete(text.getSpanStart(span), text.getSpanEnd(span) + 1);
  }
 public void afterTextChanged(Editable edt) {
   String temp = edt.toString();
   if (temp.length() == 2 && temp.substring(0, 1).toCharArray()[0] > 53) {
     edt.delete(1, 2);
   }
 }
        @Override
        public void onKey(int primaryCode, int[] keyCodes) {

          EditText editText = getTarget();

          if (editText == null) {
            // Nothing to write to.
            return;
          }

          Editable editable = editText.getText();

          if (editable == null) {
            // It can even happen?
            return;
          }

          int selStart = editText.getSelectionStart();
          int selEnd = editText.getSelectionEnd();

          if (selStart > selEnd) {
            int tmp = selStart;
            selStart = selEnd;
            selEnd = tmp;
          }

          // Apply the key to the EditText
          switch (primaryCode) {
            case CodeDelete:
              if (selStart != selEnd) {
                editable.delete(selStart, selEnd);
              } else if (selStart > 0) {
                editable.delete(selStart - 1, selEnd);
              }
              break;
            case CodeFnc:
              pickFunction();
              break;
            case CodeVar:
              pickVariable();
              break;
            case CodeEnter:
              View next = editText.focusSearch(View.FOCUS_RIGHT);
              // if (next == null) {
              //	next = editText.focusSearch(View.FOCUS_DOWN);
              // }

              if (next != null) {
                next.requestFocus();
                Log.d("onKey", "Sent focus to next view.");
              } else {
                hide();
                Log.d("onKey", "Next view not found. Hide kbd.");
              }
              break;
            default:
              // Insert character
              if (selStart != selEnd) {
                editable.delete(selStart, selEnd);
              }
              editable.insert(selStart, Character.toString((char) primaryCode));
              break;
          }
        }
  protected void handleFocus(boolean hasFocus) {
    if (!hasFocus) {
      setSingleLine(true);

      Editable text = getText();
      if (text != null && lastLayout != null) {
        // Display +x thingy if appropriate
        int lastPosition = lastLayout.getLineVisibleEnd(0);
        TokenImageSpan[] tokens = text.getSpans(0, lastPosition, TokenImageSpan.class);
        int count = objects.size() - tokens.length;
        if (count > 0) {
          lastPosition++;
          CountSpan cs =
              new CountSpan(
                  count,
                  getContext(),
                  getCurrentTextColor(),
                  (int) getTextSize(),
                  (int) maxTextWidth());
          text.insert(lastPosition, cs.text);

          float newWidth =
              Layout.getDesiredWidth(
                  text, 0, lastPosition + cs.text.length(), lastLayout.getPaint());
          // If the +x span will be moved off screen, move it one token in
          if (newWidth > maxTextWidth()) {
            text.delete(lastPosition, lastPosition + cs.text.length());

            if (tokens.length > 0) {
              TokenImageSpan token = tokens[tokens.length - 1];
              lastPosition = text.getSpanStart(token);
              cs.setCount(count + 1);
            } else {
              lastPosition = prefix.length();
            }

            text.insert(lastPosition, cs.text);
          }

          text.setSpan(
              cs, lastPosition, lastPosition + cs.text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
      }

    } else {
      setSingleLine(false);
      Editable text = getText();
      if (text != null) {
        CountSpan[] counts = text.getSpans(0, text.length(), CountSpan.class);
        for (CountSpan count : counts) {
          text.delete(text.getSpanStart(count), text.getSpanEnd(count));
          text.removeSpan(count);
        }

        if (hintVisible) {
          setSelection(prefix.length());
        } else {
          setSelection(text.length());
        }

        TokenSpanWatcher[] watchers =
            getText().getSpans(0, getText().length(), TokenSpanWatcher.class);
        if (watchers.length == 0) {
          // Someone removes watchers? I'm pretty sure this isn't in this code... -mgod
          text.setSpan(spanWatcher, 0, text.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        }
      }
    }
  }
  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if ((requestCode == LinkDialogFragment.LINK_DIALOG_REQUEST_CODE_ADD
        || requestCode == LinkDialogFragment.LINK_DIALOG_REQUEST_CODE_UPDATE)) {

      if (resultCode == LinkDialogFragment.LINK_DIALOG_REQUEST_CODE_DELETE) {
        mWebView.execJavaScriptFromString("ZSSEditor.unlink();");
        return;
      }

      if (data == null) {
        return;
      }

      Bundle extras = data.getExtras();
      if (extras == null) {
        return;
      }

      String linkUrl = extras.getString("linkURL");
      String linkText = extras.getString("linkText");

      if (linkText == null || linkText.equals("")) {
        linkText = linkUrl;
      }

      if (mSourceView.getVisibility() == View.VISIBLE) {
        Editable content = mSourceViewContent.getText();
        if (content == null) {
          return;
        }

        if (mSelectionStart < mSelectionEnd) {
          content.delete(mSelectionStart, mSelectionEnd);
        }

        String urlHtml = "<a href=\"" + linkUrl + "\">" + linkText + "</a>";

        content.insert(mSelectionStart, urlHtml);
        mSourceViewContent.setSelection(mSelectionStart + urlHtml.length());
      } else {
        String jsMethod;
        if (requestCode == LinkDialogFragment.LINK_DIALOG_REQUEST_CODE_ADD) {
          jsMethod = "ZSSEditor.insertLink";
        } else {
          jsMethod = "ZSSEditor.updateLink";
        }
        mWebView.execJavaScriptFromString(
            jsMethod
                + "('"
                + Utils.escapeHtml(linkUrl)
                + "', '"
                + Utils.escapeHtml(linkText)
                + "');");
      }
    } else if (requestCode == ImageSettingsDialogFragment.IMAGE_SETTINGS_DIALOG_REQUEST_CODE) {
      if (data == null) {
        return;
      }

      Bundle extras = data.getExtras();
      if (extras == null) {
        return;
      }

      final String imageMeta = extras.getString("imageMeta");
      final int imageRemoteId = extras.getInt("imageRemoteId");
      final boolean isFeaturedImage = extras.getBoolean("isFeatured");

      mWebView.post(
          new Runnable() {
            @Override
            public void run() {
              mWebView.execJavaScriptFromString(
                  "ZSSEditor.updateCurrentImageMeta('" + imageMeta + "');");
            }
          });

      if (imageRemoteId != 0) {
        if (isFeaturedImage) {
          mFeaturedImageId = imageRemoteId;
          mEditorFragmentListener.onFeaturedImageChanged(mFeaturedImageId);
        } else {
          // If this image was unset as featured, clear the featured image id
          if (mFeaturedImageId == imageRemoteId) {
            mFeaturedImageId = 0;
            mEditorFragmentListener.onFeaturedImageChanged(mFeaturedImageId);
          }
        }
      }
    }
  }