亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

想要使用 StringBuilder 添加帶有 TextView 數字的上標

想要使用 StringBuilder 添加帶有 TextView 數字的上標

達令說 2022-08-17 17:18:08
public class SuperscriptFormatter {    private static final String SUPERSCRIPT_REGEX = "(?<=\\b\\d{0,10})(st|nd|rd|th)(?=\\b)";    private static final Pattern PATTERN = Pattern.compile(SUPERSCRIPT_REGEX);    private static final float PROPORTION = 0.5f;    private final SpannableStringBuilder stringBuilder;    public SuperscriptFormatter (@NonNull SpannableStringBuilder stringBuilder) {        this.stringBuilder = stringBuilder;    }    public void format(TextView textView) {        CharSequence text = textView.getText();        Matcher matcher = PATTERN.matcher(text);        stringBuilder.clear();        stringBuilder.append(text);        while (matcher.find()) {            int start = matcher.start();            int end = matcher.end();            createSuperscriptSpan(start, end);        }        textView.setText(stringBuilder);    }    private void createSuperscriptSpan(int start, int end) {        SuperscriptSpan superscript = new SuperscriptSpan();        RelativeSizeSpan size = new RelativeSizeSpan(PROPORTION);        stringBuilder.setSpan(superscript, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);        stringBuilder.setSpan(size, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);    }}問題是我無法獲得帶有數字的上標。我傳遞“1”,“2”作為文本,每次都返回假,我不知道模式或文本發生了什么。我想要1st,2nd 3rd ...等作為文本matcher.find()
查看完整描述

2 回答

?
四季花海

TA貢獻1811條經驗 獲得超5個贊

在上面的代碼中,您必須在活動類中設置文本,或者在布局文件夾中.xml文件。

textView.setText( "21st, 22nd, 11th, 13th");

或 xml 文件文本視圖

android:text= "21st, 22nd, 11th, 13th"

像這樣,那里沒有自動放置上標。


查看完整回答
反對 回復 2022-08-17
?
三國紛爭

TA貢獻1804條經驗 獲得超7個贊

我已經編寫了自己的邏輯來添加帶有數字的上標。在這里,我正在使用kotlin在RecyclerView適配器上工作,我正在共享onBindViewHolder的代碼。


    var stringBuilder: SpannableStringBuilder = SpannableStringBuilder()



  override fun onBindViewHolder(holder: ViewHolder?, position: Int) {

    // for 1 till 9

    if (yourNo  in 1..9) {

        if (yourNo  == 1)

            changeSuperScripts(1, 3, yourNo + "st", holder.textView)

        else if (contestStandingItem.rank == 2)

            changeSuperScripts(1, 3, yourNo + "nd", holder.textView)

        else if (contestStandingItem.rank == 3) {

            changeSuperScripts(1, 3, yourNo + "rd", holder.textView)

        } else

            changeSuperScripts(1, 3, yourNo + "th", holder.textView)

    }


    // for 10 till 19

    else if (yourNo  in 10..19)

        changeSuperScripts(2, 4, yourNo  + "th", holder.textView)



    // for 20 till 99

    else if(yourNo  in 20..99) {

         if (yourNo  % 10 == 0)

            changeSuperScripts(2, 4, yourNo  + "th", holder.textView)

        else if (yourNo  % 10 == 1)

            changeSuperScripts(2, 4, yourNo  + "st", holder.textView)

        else if (yourNo  % 10 == 2)

            changeSuperScripts(2, 4, yourNo  + "nd", holder.textView)

        else if (yourNo  % 10 == 3)

            changeSuperScripts(2, 4, yourNo  + "rd", holder.textView)

        else

            changeSuperScripts(2, 4, yourNo  + "th", holder.textView)

    }


    // for 100 till 999

    else if (yourNo  in 100..999){

        if (yourNo  % 10 == 0)

            changeSuperScripts(3, 5, yourNo  + "th", holder.textView)

        else if (yourNo  % 10 == 1)

            changeSuperScripts(3, 5, yourNo  + "st", holder.textView)

        else if (yourNo  % 10 == 2)

            changeSuperScripts(3, 5, yourNo  + "nd", holder.textView)

        else if (yourNo  % 10 == 3)

            changeSuperScripts(3, 5, yourNo  + "rd", holder.textView)

        else

            changeSuperScripts(3, 5, yourNo  + "th", holder.textView)

    }

}


開始和結束是超級裂縫開始和停止的索引


     fun changeSuperScripts(start: Int, end: Int, text: String, textView: TextView) {

    stringBuilder.clear()

    stringBuilder.append(text)

    val formatter = OrdinalSuperscriptFormatter.getInstance(stringBuilder)

    formatter.createSuperscriptSpan(start, end, textView)

}

這是一個用java編寫的單獨類,用于將文本轉換為上標,我已經將其設置為每次使用相同的對象的單例(僅用于性能)


公共類 OrdinalSuperscriptFormatter {


public static OrdinalSuperscriptFormatter ordinalSuperscriptFormatter;


private static final String SUPERSCRIPT_REGEX = "(?<=\\b\\d{0,10})(st|nd|rd|th)(?=\\b)";

private static final Pattern PATTERN = Pattern.compile(SUPERSCRIPT_REGEX);

private static final float PROPORTION = 0.5f;

SuperscriptSpan superscript = new SuperscriptSpan();

RelativeSizeSpan size = new RelativeSizeSpan(PROPORTION);


private final SpannableStringBuilder stringBuilder;


public static OrdinalSuperscriptFormatter getInstance(SpannableStringBuilder stringBuilder){

    if (ordinalSuperscriptFormatter == null) {

        ordinalSuperscriptFormatter = new OrdinalSuperscriptFormatter(stringBuilder);

        return  ordinalSuperscriptFormatter;

    }

    else

        return  ordinalSuperscriptFormatter;

}


public static void refreshInstance(){

    ordinalSuperscriptFormatter = null;

}


private OrdinalSuperscriptFormatter(@NonNull SpannableStringBuilder stringBuilder) {

    this.stringBuilder = stringBuilder;

}


public void createSuperscriptSpan(int start, int end,TextView textView) {

    stringBuilder.setSpan(superscript, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    stringBuilder.setSpan(size, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    textView.setText(stringBuilder);

}

}


refreshInstance 將在您開始活動時調用,或者再次斷開以刷新列表


   override fun onResume() {

    super.onResume()

    OrdinalSuperscriptFormatter.refreshInstance() // must be referesh singleton instance for each history standings

}


查看完整回答
反對 回復 2022-08-17
  • 2 回答
  • 0 關注
  • 170 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號