當前位置: 首頁>>代碼示例>>Java>>正文


Java SpannableStringBuilder.insert方法代碼示例

本文整理匯總了Java中android.text.SpannableStringBuilder.insert方法的典型用法代碼示例。如果您正苦於以下問題:Java SpannableStringBuilder.insert方法的具體用法?Java SpannableStringBuilder.insert怎麽用?Java SpannableStringBuilder.insert使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在android.text.SpannableStringBuilder的用法示例。


在下文中一共展示了SpannableStringBuilder.insert方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: generateStaticLayout

import android.text.SpannableStringBuilder; //導入方法依賴的package包/類
public static StaticLayout generateStaticLayout(CharSequence text, TextPaint paint, int maxWidth, int smallWidth, int linesCount, int maxLines) {
    SpannableStringBuilder stringBuilder = new SpannableStringBuilder(text);
    int addedChars = 0;
    StaticLayout layout = new StaticLayout(text, paint, smallWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
    for (int a = 0; a < linesCount; a++) {
        Layout.Directions directions = layout.getLineDirections(a);
        if (layout.getLineLeft(a) != 0 || layout.isRtlCharAt(layout.getLineStart(a)) || layout.isRtlCharAt(layout.getLineEnd(a))) {
            maxWidth = smallWidth;
        }
        int pos = layout.getLineEnd(a);
        if (pos == text.length()) {
            break;
        }
        pos--;
        if (stringBuilder.charAt(pos + addedChars) == ' ') {
            stringBuilder.replace(pos + addedChars, pos + addedChars + 1, "\n");
        } else if (stringBuilder.charAt(pos + addedChars) != '\n') {
            stringBuilder.insert(pos + addedChars, "\n");
            addedChars++;
        }
        if (a == layout.getLineCount() - 1 || a == maxLines - 1) {
            break;
        }
    }
    return StaticLayoutEx.createStaticLayout(stringBuilder, paint, maxWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, dp(1), false, TextUtils.TruncateAt.END, maxWidth, maxLines);
}
 
開發者ID:MLNO,項目名稱:airgram,代碼行數:27,代碼來源:ChatMessageCell.java

示例2: addSpacesAroundSpansUntilFixed

import android.text.SpannableStringBuilder; //導入方法依賴的package包/類
private FixingResult addSpacesAroundSpansUntilFixed(SpannableStringBuilder builder,
                                                    int widthMeasureSpec, int heightMeasureSpec) {

    Object[] spans = builder.getSpans(0, builder.length(), Object.class);
    List<Object> spansWithSpacesBefore = new ArrayList<>(spans.length);
    List<Object> spansWithSpacesAfter = new ArrayList<>(spans.length);

    for (Object span : spans) {
        int spanStart = builder.getSpanStart(span);
        if (isNotSpace(builder, spanStart - 1)) {
            builder.insert(spanStart, " ");
            spansWithSpacesBefore.add(span);
        }

        int spanEnd = builder.getSpanEnd(span);
        if (isNotSpace(builder, spanEnd)) {
            builder.insert(spanEnd, " ");
            spansWithSpacesAfter.add(span);
        }

        try {
            setTextAndMeasure(builder, widthMeasureSpec, heightMeasureSpec);
            return FixingResult.fixed(spansWithSpacesBefore, spansWithSpacesAfter);
        } catch (IndexOutOfBoundsException ignored) {
        }
    }
    if (HtmlTextView.DEBUG) {
        Log.d(HtmlTextView.TAG, "Could not fix the Spanned by adding spaces around spans");
    }
    return FixingResult.notFixed();
}
 
開發者ID:RanKKI,項目名稱:PSNine,代碼行數:32,代碼來源:JellyBeanSpanFixTextView.java

示例3: setEmoteSpans

import android.text.SpannableStringBuilder; //導入方法依賴的package包/類
private void setEmoteSpans(SpannableStringBuilder builder) {
    for (URLSpan span : builder.getSpans(0, builder.length(), URLSpan.class)) {
            setLargeLinks(builder, span);
        File emoteDir = new File(Environment.getExternalStorageDirectory(), "RedditEmotes");
        File emoteFile = new File(emoteDir, span.getURL().replace("/", "").replaceAll("-.*", "")
                + ".png"); //BPM uses "-" to add dynamics for emotes in browser. Fall back to original here if exists.
        boolean startsWithSlash = span.getURL().startsWith("/");
        boolean hasOnlyOneSlash = StringUtils.countMatches(span.getURL(), "/") == 1;

        if (emoteDir.exists() && startsWithSlash && hasOnlyOneSlash && emoteFile.exists()) {
            //We've got an emote match
            int start = builder.getSpanStart(span);
            int end = builder.getSpanEnd(span);
            CharSequence textCovers = builder.subSequence(start, end);

            //Make sure bitmap loaded works well with screen density.
            BitmapFactory.Options options = new BitmapFactory.Options();
            DisplayMetrics metrics = new DisplayMetrics();
            ((WindowManager) getContext().getSystemService(
                    Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(metrics);
            options.inDensity = 240;
            options.inScreenDensity = metrics.densityDpi;
            options.inScaled = true;

            //Since emotes are not directly attached to included text, add extra character to attach image to.
            builder.removeSpan(span);
            if (builder.subSequence(start, end).charAt(0) != '.') {
                builder.insert(start, ".");
            }
            Bitmap emoteBitmap = BitmapFactory.decodeFile(emoteFile.getAbsolutePath(), options);
            builder.setSpan(new ImageSpan(getContext(), emoteBitmap), start, start + 1,
                    Spanned.SPAN_INCLUSIVE_INCLUSIVE);
            //Check if url span has length. If it does, it's a spoiler/caption
            if (textCovers.length() > 1) {
                builder.setSpan(new URLSpan("/sp"), start + 1, end + 1,
                        Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                builder.setSpan(new StyleSpan(Typeface.ITALIC), start + 1, end + 1,
                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            builder.append("\n"); //Newline to fix text wrapping issues
        }
    }
}
 
開發者ID:ccrama,項目名稱:Slide-RSS,代碼行數:44,代碼來源:SpoilerRobotoTextView.java

示例4: updateView

import android.text.SpannableStringBuilder; //導入方法依賴的package包/類
private void updateView() {
    final MonetaryFormat btcFormat = config.getFormat();

    if (walletToSweep != null) {
        balanceView.setVisibility(View.VISIBLE);
        final MonetarySpannable balanceSpannable = new MonetarySpannable(btcFormat,
                walletToSweep.getBalance(BalanceType.ESTIMATED));
        balanceSpannable.applyMarkup(null, null);
        final SpannableStringBuilder balance = new SpannableStringBuilder(balanceSpannable);
        balance.insert(0, ": ");
        balance.insert(0, getString(R.string.sweep_wallet_fragment_balance));
        balanceView.setText(balance);
    } else {
        balanceView.setVisibility(View.GONE);
    }

    if (state == State.DECODE_KEY && privateKeyToSweep == null) {
        messageView.setVisibility(View.VISIBLE);
        messageView.setText(R.string.sweep_wallet_fragment_wallet_unknown);
    } else if (state == State.DECODE_KEY && privateKeyToSweep != null) {
        messageView.setVisibility(View.VISIBLE);
        messageView.setText(R.string.sweep_wallet_fragment_encrypted);
    } else if (privateKeyToSweep != null) {
        messageView.setVisibility(View.GONE);
    }

    passwordViewGroup
            .setVisibility(state == State.DECODE_KEY && privateKeyToSweep != null ? View.VISIBLE : View.GONE);

    hintView.setVisibility(state == State.DECODE_KEY && privateKeyToSweep == null ? View.VISIBLE : View.GONE);

    if (sentTransaction != null) {
        sweepTransactionView.setVisibility(View.VISIBLE);
        sweepTransactionAdapter.setFormat(btcFormat);
        sweepTransactionAdapter.replace(sentTransaction);
        sweepTransactionAdapter.bindViewHolder(sweepTransactionViewHolder, 0);
    } else {
        sweepTransactionView.setVisibility(View.GONE);
    }

    if (state == State.DECODE_KEY) {
        viewCancel.setText(R.string.button_cancel);
        viewGo.setText(R.string.sweep_wallet_fragment_button_decrypt);
        viewGo.setEnabled(privateKeyToSweep != null);
    } else if (state == State.CONFIRM_SWEEP) {
        viewCancel.setText(R.string.button_cancel);
        viewGo.setText(R.string.sweep_wallet_fragment_button_sweep);
        viewGo.setEnabled(walletToSweep != null && walletToSweep.getBalance(BalanceType.ESTIMATED).signum() > 0
                && fees != null);
    } else if (state == State.PREPARATION) {
        viewCancel.setText(R.string.button_cancel);
        viewGo.setText(R.string.send_coins_preparation_msg);
        viewGo.setEnabled(false);
    } else if (state == State.SENDING) {
        viewCancel.setText(R.string.send_coins_fragment_button_back);
        viewGo.setText(R.string.send_coins_sending_msg);
        viewGo.setEnabled(false);
    } else if (state == State.SENT) {
        viewCancel.setText(R.string.send_coins_fragment_button_back);
        viewGo.setText(R.string.send_coins_sent_msg);
        viewGo.setEnabled(false);
    } else if (state == State.FAILED) {
        viewCancel.setText(R.string.send_coins_fragment_button_back);
        viewGo.setText(R.string.send_coins_failed_msg);
        viewGo.setEnabled(false);
    }

    viewCancel.setEnabled(state != State.PREPARATION);

    // enable actions
    if (reloadAction != null)
        reloadAction.setEnabled(state == State.CONFIRM_SWEEP && walletToSweep != null);
    if (scanAction != null)
        scanAction.setEnabled(state == State.DECODE_KEY || state == State.CONFIRM_SWEEP);
}
 
開發者ID:guodroid,項目名稱:okwallet,代碼行數:76,代碼來源:SweepWalletFragment.java

示例5: pickSuggestionManually

import android.text.SpannableStringBuilder; //導入方法依賴的package包/類
public void pickSuggestionManually(int index) {
    if (index < 0) {
        // assert
        return;
    }

    if (mCompletionOn && mCompletions != null && index < mCompletions.length) {
        CompletionInfo ci = mCompletions[index];
        getCurrentInputConnection().commitCompletion(ci);
        if (mCandidateView != null) {
            mCandidateView.clear();
        }
        return;
    }
    
    if (mComposing.length() > 0) {
        String s;
        // Commit the candidate suggestion for the current text.
        if(index < mSuggestions.size()) {
            s = mSuggestions.get(index);
            getCurrentInputConnection().commitText(s, s.length());
        }
        else if (index < mQuranSuggestions.size()) {
            AyaMatch m = mQuranSuggestions.get(index);
            SpannableStringBuilder txt = new SpannableStringBuilder(m.strBld);
            int len = m.len;

            txt.insert(0, "﴿");
            len++;
            txt.insert(len - m.slen, "﴾");
            len++;

            // separate Quran from following text with a dot
            txt.append('.');
            len++;
            if (getPrefRasm() == Rasm.UTHMANI /*&& mUthmaniTypeFace != null*/) {
                txt.setSpan(new CustomTypefaceSpan(getUthmaniTypeFace()), 0, len, 0);
            }
            Toast.makeText(this, txt.subSequence(0, len), Toast.LENGTH_LONG).show();

            for (int i = 0; i < mSavedPreSpaces; i++) txt.insert(0, " ");
            len += mSavedPreSpaces;
            getCurrentInputConnection().commitText(txt.subSequence(0, len), len);
            mSavedPreSpaces = 0;
        }
        mComposing.setLength(0);
        updateCandidates();
    }
}
 
開發者ID:cdjalel,項目名稱:QuranKeyboard,代碼行數:50,代碼來源:QuranKeyboardIME.java


注:本文中的android.text.SpannableStringBuilder.insert方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。