List of usage examples for android.text Editable subSequence
CharSequence subSequence(int start, int end);
From source file:org.peterbaldwin.client.android.delicious.WebPageTitleRequest.java
/** * {@inheritDoc}//from www.j a va2 s. c om */ public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) { if ("title".equalsIgnoreCase(tag)) { if (opening) { mStart = output.length(); } else { int end = output.length(); String title = output.subSequence(mStart, end).toString(); // Collapse internal whitespace title = title.replaceAll("\\s+", " "); // Remove leading/trailing space title = title.trim(); mTitle = title; throw new TerminateParser(); } } }
From source file:org.cocos2dx.lib.TextInputWraper.java
@Override public void afterTextChanged(Editable s) { if (isFullScreenEdit()) { return;/*from w w w .j av a2s . co m*/ } LogD("afterTextChanged: " + s); int nModified = s.length() - mText.length(); if (nModified > 0) { final String insertText = s.subSequence(mText.length(), s.length()).toString(); mMainView.insertText(insertText); LogD("insertText(" + insertText + ")"); } else { for (; nModified < 0; ++nModified) { mMainView.deleteBackward(); LogD("deleteBackward"); } } mText = s.toString(); }
From source file:org.mozilla.focus.widget.InlineAutocompleteEditText.java
public String getOriginalText() { final Editable text = getText(); return text.subSequence(0, mAutoCompletePrefixLength).toString(); }
From source file:com.duy.pascal.ui.editor.highlight.CodeHighlighter.java
private void highlightStringAndOther(@NonNull Editable allText, @NonNull CharSequence textToHighlight, int start) { stringHighlighter.highlight(allText, textToHighlight, start); ArrayList<Pair<Integer, Integer>> region = stringHighlighter.getStringRegion(); if (region.size() == 0) { highlightOther(allText, textToHighlight, start); return;/*from w ww.j a v a 2 s . c om*/ } Collections.sort(region, new Comparator<Pair<Integer, Integer>>() { @Override public int compare(Pair<Integer, Integer> o1, Pair<Integer, Integer> o2) { return o1.first.compareTo(o2.first); } }); int startIndex, endIndex; if (region.size() > 0 && start < region.get(0).first - 1) { highlightOther(allText, allText.subSequence(start, region.get(0).first - 1), start); } for (int i = 0; i < region.size() - 1; i++) { startIndex = region.get(i).second + 1; endIndex = region.get(i + 1).first - 1; if (startIndex < endIndex) { highlightOther(allText, allText.subSequence(startIndex, endIndex), startIndex); } } if (region.size() > 0) { startIndex = region.get(region.size() - 1).second + 1; endIndex = start + textToHighlight.length(); if (startIndex <= endIndex) { highlightOther(allText, allText.subSequence(startIndex, endIndex), startIndex); } } }
From source file:com.ruesga.rview.widget.TagEditTextView.java
private void createChip(Editable s, boolean nextIsTag) { int start = mTagList.size(); int end = s.length() + (nextIsTag ? -1 : 0); String tagText = s.subSequence(start, end).toString().trim(); tagText = NON_UNICODE_CHAR_PATTERN.matcher(tagText).replaceAll(""); if (tagText.isEmpty() || tagText.length() <= 1) { // User is still writing return;/*from w w w . j a va 2 s .com*/ } String charText = tagText.substring(0, 1); if (!VALID_TAGS.contains(charText) || (charText.charAt(0) == VALID_TAGS.charAt(1) && !mSupportsUserTags)) { char tag = mDefaultTagMode == TAG_MODE.HASH ? VALID_TAGS.charAt(0) : VALID_TAGS.charAt(1); tagText = tag + tagText; } // Replace the new tag s.replace(start, end, CHIP_REPLACEMENT_CHAR); // Create the tag and its spannable final Tag tag = new Tag(); tag.mTag = NON_UNICODE_CHAR_PATTERN.matcher(tagText).replaceAll(""); Bitmap b = createTagChip(tag); ImageSpan span = new ImageSpan(getContext(), b); s.setSpan(span, start, start + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); tag.w = b.getWidth(); tag.h = b.getHeight(); mTagList.add(tag); notifyTagCreated(tag); }
From source file:com.duy.pascal.ui.editor.highlight.CodeHighlighter.java
@Override public void highlight(@NonNull Editable allText, @NonNull CharSequence textToHighlight, int start) { try {// www . j ava 2 s. c o m commentHighlighter.highlight(allText, textToHighlight, start); ArrayList<Pair<Integer, Integer>> region = commentHighlighter.getCommentRegion(); if (region.size() == 0) { highlightStringAndOther(allText, textToHighlight, start); return; } Collections.sort(region, new Comparator<Pair<Integer, Integer>>() { @Override public int compare(Pair<Integer, Integer> o1, Pair<Integer, Integer> o2) { return o1.first.compareTo(o2.first); } }); int startIndex, endIndex; if (region.size() > 0 && start < region.get(0).first - 1) { highlightStringAndOther(allText, allText.subSequence(start, region.get(0).first - 1), start); } for (int i = 0; i < region.size() - 1; i++) { startIndex = region.get(i).second + 1; endIndex = region.get(i + 1).first - 1; if (startIndex < endIndex) { highlightStringAndOther(allText, allText.subSequence(startIndex, endIndex), startIndex); } } if (region.size() > 0) { startIndex = region.get(region.size() - 1).second + 1; endIndex = start + textToHighlight.length(); if (startIndex <= endIndex) { highlightStringAndOther(allText, allText.subSequence(startIndex, endIndex), startIndex); } } } catch (Exception ignored) { ignored.printStackTrace(); } }
From source file:tv.acfun.a63.CommentsActivity.java
String getComment() { Editable text = SpannableStringBuilder.valueOf(mCommentText.getText()); Quote quote = TextViewUtils.getLast(text, Quote.class); int start = text.getSpanStart(quote); int end = text.getSpanEnd(quote); if (start < 0) return text.toString(); else if (start == 0) { return text.subSequence(end, text.length()).toString(); } else/*ww w. j a va 2 s .co m*/ return text.subSequence(0, start).toString() + text.subSequence(end, text.length()).toString(); }
From source file:com.krayzk9s.imgurholo.ui.SingleImageFragment.java
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); final ImgurHoloActivity activity = (ImgurHoloActivity) getActivity(); final SharedPreferences settings = activity.getApiCall().settings; sort = settings.getString("CommentSort", "Best"); boolean newData = true; if (commentData != null) { newData = false;/*from www. jav a 2 s.c o m*/ } mainView = inflater.inflate(R.layout.single_image_layout, container, false); String[] mMenuList = getResources().getStringArray(R.array.emptyList); if (commentAdapter == null) commentAdapter = new CommentAdapter(mainView.getContext()); commentLayout = (ListView) mainView.findViewById(R.id.comment_thread); commentLayout.setChoiceMode(ListView.CHOICE_MODE_SINGLE); if (settings.getString("theme", MainActivity.HOLO_LIGHT).equals(MainActivity.HOLO_LIGHT)) imageLayoutView = (LinearLayout) View.inflate(getActivity(), R.layout.image_view, null); else imageLayoutView = (LinearLayout) View.inflate(getActivity(), R.layout.dark_image_view, null); mPullToRefreshLayout = (PullToRefreshLayout) mainView.findViewById(R.id.ptr_layout); ActionBarPullToRefresh.from(getActivity()) // Mark All Children as pullable .allChildrenArePullable() // Set the OnRefreshListener .listener(this) // Finally commit the setup to our PullToRefreshLayout .setup(mPullToRefreshLayout); if (savedInstanceState != null && newData) { imageData = savedInstanceState.getParcelable("imageData"); inGallery = savedInstanceState.getBoolean("inGallery"); } LinearLayout layout = (LinearLayout) imageLayoutView.findViewById(R.id.image_buttons); TextView imageDetails = (TextView) imageLayoutView.findViewById(R.id.single_image_details); layout.setVisibility(View.VISIBLE); ImageButton imageFullscreen = (ImageButton) imageLayoutView.findViewById(R.id.fullscreen); imageUpvote = (ImageButton) imageLayoutView.findViewById(R.id.rating_good); imageDownvote = (ImageButton) imageLayoutView.findViewById(R.id.rating_bad); ImageButton imageFavorite = (ImageButton) imageLayoutView.findViewById(R.id.rating_favorite); imageComment = (ImageButton) imageLayoutView.findViewById(R.id.comment); ImageButton imageUser = (ImageButton) imageLayoutView.findViewById(R.id.user); imageScore = (TextView) imageLayoutView.findViewById(R.id.single_image_score); TextView imageInfo = (TextView) imageLayoutView.findViewById(R.id.single_image_info); Log.d("imageData", imageData.getJSONObject().toString()); if (imageData.getJSONObject().has("ups")) { imageUpvote.setVisibility(View.VISIBLE); imageDownvote.setVisibility(View.VISIBLE); imageScore.setVisibility(View.VISIBLE); imageComment.setVisibility(View.VISIBLE); ImageUtils.updateImageFont(imageData, imageScore); } imageInfo.setVisibility(View.VISIBLE); ImageUtils.updateInfoFont(imageData, imageInfo); imageUser.setVisibility(View.VISIBLE); imageFavorite.setVisibility(View.VISIBLE); try { if (!imageData.getJSONObject().has("account_url") || imageData.getJSONObject().getString("account_url").equals("null") || imageData.getJSONObject().getString("account_url").equals("[deleted]")) imageUser.setVisibility(View.GONE); if (!imageData.getJSONObject().has("vote")) { imageUpvote.setVisibility(View.GONE); imageDownvote.setVisibility(View.GONE); } else { if (imageData.getJSONObject().getString("vote") != null && imageData.getJSONObject().getString("vote").equals("up")) imageUpvote.setImageResource(R.drawable.green_rating_good); else if (imageData.getJSONObject().getString("vote") != null && imageData.getJSONObject().getString("vote").equals("down")) imageDownvote.setImageResource(R.drawable.red_rating_bad); } if (imageData.getJSONObject().getString("favorite") != null && imageData.getJSONObject().getBoolean("favorite")) imageFavorite.setImageResource(R.drawable.green_rating_favorite); } catch (JSONException e) { Log.e("Error!", e.toString()); } imageFavorite.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { ImageUtils.favoriteImage(singleImageFragment, imageData, (ImageButton) view, activity.getApiCall()); } }); imageUser.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { ImageUtils.gotoUser(singleImageFragment, imageData); } }); imageComment.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Activity activity = getActivity(); final EditText newBody = new EditText(activity); newBody.setHint(R.string.body_hint_body); newBody.setLines(3); final TextView characterCount = new TextView(activity); characterCount.setText("140"); LinearLayout commentReplyLayout = new LinearLayout(activity); newBody.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) { // } @Override public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) { characterCount.setText(String.valueOf(140 - charSequence.length())); } @Override public void afterTextChanged(Editable editable) { for (int i = editable.length(); i > 0; i--) { if (editable.subSequence(i - 1, i).toString().equals("\n")) editable.replace(i - 1, i, ""); } } }); commentReplyLayout.setOrientation(LinearLayout.VERTICAL); commentReplyLayout.addView(newBody); commentReplyLayout.addView(characterCount); new AlertDialog.Builder(activity).setTitle(R.string.dialog_comment_on_image_title) .setView(commentReplyLayout) .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { if (newBody.getText() != null && newBody.getText().toString().length() < 141) { HashMap<String, Object> commentMap = new HashMap<String, Object>(); try { commentMap.put("comment", newBody.getText().toString()); commentMap.put("image_id", imageData.getJSONObject().getString("id")); Fetcher fetcher = new Fetcher(singleImageFragment, "3/comment/", ApiCall.POST, commentMap, ((ImgurHoloActivity) getActivity()).getApiCall(), POSTCOMMENT); fetcher.execute(); } catch (JSONException e) { Log.e("Error!", e.toString()); } } } }).setNegativeButton(R.string.dialog_answer_cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { // Do nothing. } }).show(); } }); imageUpvote.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { ImageUtils.upVote(singleImageFragment, imageData, imageUpvote, imageDownvote, activity.getApiCall()); ImageUtils.updateImageFont(imageData, imageScore); } }); imageDownvote.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { ImageUtils.downVote(singleImageFragment, imageData, imageUpvote, imageDownvote, activity.getApiCall()); ImageUtils.updateImageFont(imageData, imageScore); } }); if (popupWindow != null) { popupWindow.dismiss(); } popupWindow = new PopupWindow(); imageFullscreen.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { ImageUtils.fullscreen(singleImageFragment, imageData, popupWindow, mainView); } }); ArrayAdapter<String> tempAdapter = new ArrayAdapter<String>(mainView.getContext(), R.layout.drawer_list_item, mMenuList); Log.d("URI", "YO I'M IN YOUR SINGLE FRAGMENT gallery:" + inGallery); imageView = (ImageView) imageLayoutView.findViewById(R.id.single_image_view); loadImage(); TextView imageTitle = (TextView) imageLayoutView.findViewById(R.id.single_image_title); TextView imageDescription = (TextView) imageLayoutView.findViewById(R.id.single_image_description); try { String size = String .valueOf(NumberFormat.getIntegerInstance() .format(imageData.getJSONObject().getInt(ImgurHoloActivity.IMAGE_DATA_WIDTH))) + "x" + NumberFormat.getIntegerInstance() .format(imageData.getJSONObject().getInt(ImgurHoloActivity.IMAGE_DATA_HEIGHT)) + " (" + NumberFormat.getIntegerInstance() .format(imageData.getJSONObject().getInt(ImgurHoloActivity.IMAGE_DATA_SIZE)) + "B)"; String initial = imageData.getJSONObject().getString(ImgurHoloActivity.IMAGE_DATA_TYPE) + " " + Html.fromHtml("•") + " " + size + " " + Html.fromHtml("•") + " " + "Views: " + NumberFormat.getIntegerInstance() .format(imageData.getJSONObject().getInt(ImgurHoloActivity.IMAGE_DATA_VIEWS)); imageDetails.setText(initial); Log.d("imagedata", imageData.getJSONObject().toString()); if (!imageData.getJSONObject().getString(ImgurHoloActivity.IMAGE_DATA_TITLE).equals("null")) imageTitle.setText(imageData.getJSONObject().getString(ImgurHoloActivity.IMAGE_DATA_TITLE)); else imageTitle.setVisibility(View.GONE); if (!imageData.getJSONObject().getString(ImgurHoloActivity.IMAGE_DATA_DESCRIPTION).equals("null")) { imageDescription .setText(imageData.getJSONObject().getString(ImgurHoloActivity.IMAGE_DATA_DESCRIPTION)); imageDescription.setVisibility(View.VISIBLE); } else imageDescription.setVisibility(View.GONE); commentLayout.addHeaderView(imageLayoutView); commentLayout.setAdapter(tempAdapter); } catch (JSONException e) { Log.e("Text Error!", e.toString()); } if ((savedInstanceState == null || commentData == null) && newData) { commentData = new JSONParcelable(); getComments(); commentLayout.setAdapter(commentAdapter); } else if (newData) { commentArray = savedInstanceState.getParcelableArrayList("commentData"); commentAdapter.addAll(commentArray); commentLayout.setAdapter(commentAdapter); commentAdapter.notifyDataSetChanged(); } else if (commentArray != null) { commentAdapter.addAll(commentArray); commentLayout.setAdapter(commentAdapter); commentAdapter.notifyDataSetChanged(); } return mainView; }
From source file:me.albertonicoletti.latex.activities.EditorActivity.java
/** * Used to maintain the same indentation as the upper line * @param editable Text//from www . j ava 2 s .com * @param spannable Spannable * @param span Modified span */ private void autoIndentAndTabEditor(Editable editable, SpannableString spannable, RelativeSizeSpan span) { int beginIndex = spannable.getSpanStart(span); int endIndex = spannable.getSpanEnd(span); // If the last written character is a newline if (editable.length() > 0) { if (editable.charAt(endIndex - 1) == '\n') { int lineModified = editor.getLayout().getLineForOffset(beginIndex); int modifiedBeginIndex = editor.getLayout().getLineStart(lineModified); int modifiedEndIndex = editor.getLayout().getLineEnd(lineModified); String str = editable.subSequence(modifiedBeginIndex, modifiedEndIndex).toString(); // Collects the whitespaces and tabulations in the upper line String whitespaces = ""; int i = 0; while (str.charAt(i) == ' ' || str.charAt(i) == '\t') { whitespaces += str.charAt(i); i++; } // And inserts them in the newline editable.insert(beginIndex + 1, whitespaces); } if (editable.charAt(endIndex - 1) == '\t') { int tabSize = Integer.valueOf(PreferenceManager.getDefaultSharedPreferences(this) .getString(SettingsActivity.TAB_SIZE, "")); String whitespaces = ""; for (int i = 0; i < tabSize; i++) { whitespaces += " "; } editable.replace(beginIndex, beginIndex + 1, whitespaces); } } }
From source file:org.zywx.wbpalmstar.plugin.chatkeyboard.ACEChatKeyboardView.java
public void insertTextByKeyword(String keyword, String insertText, String color, boolean isReplaceKeyword) { if (TextUtils.isEmpty(keyword) || TextUtils.isEmpty(insertText)) { return;//from w ww . j a v a 2 s.c om } if (TextUtils.isEmpty(color)) { color = "#507daf"; } Editable editable = mEditText.getText(); int lastAtPosition = mLastAtPosition; if (editable.length() < keyword.length() || editable.length() < lastAtPosition + keyword.length()) { return; } if (!keyword.equals(editable.subSequence(lastAtPosition, lastAtPosition + keyword.length()).toString())) { if (!editable.toString().contains(keyword)) { return; } lastAtPosition = editable.toString().lastIndexOf(keyword); } if (isReplaceKeyword) { editable.replace(lastAtPosition, lastAtPosition + keyword.length(), Html.fromHtml("<font color=\"" + color + "\">" + insertText + "</font>")); } else { editable.insert(lastAtPosition + keyword.length(), Html.fromHtml("<font color=\"" + color + "\">" + insertText + "</font>")); } lastAtPosition += insertText.length() + (isReplaceKeyword ? 0 : keyword.length()); mEditText.setSelection(lastAtPosition); }