第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

調(diào)用 setText 時(shí)設(shè)置 TextView 的更多屬性

調(diào)用 setText 時(shí)設(shè)置 TextView 的更多屬性

守著一只汪 2023-11-01 21:53:52
我正在尋找一種簡(jiǎn)單的方法來根據(jù) TextView.setText() 設(shè)置的輸入設(shè)置 TextView 元素的更多屬性。具體來說,我的代碼目前如下所示:TextView payment;BigDecimal mBigDecimal;payment.setText(BigDecimal.toString());if (mBigDecimal.compareTo(BigDecimal.ZERO) == -1) {    payment.setBackgroundTintList(new ColorStateListStud().getList(ContextCompat.getColor(super.getContext(), R.color.lightred)));} else if (mBigDecimal.compareTo(BigDecimal.ZERO) == 1) {    payment.setBackgroundTintList(new ColorStateListStud().getList(ContextCompat.getColor(super.getContext(), R.color.lightgreen)));} else {    payment.setBackgroundTintList(new ColorStateListStud().getList(ContextCompat.getColor(super.getContext(), R.color.transparent)));}// ColorStateListStud only holds state_enabled and sets the given color這在我的整個(gè)代碼中被復(fù)制了多次。所以我想盡可能地簡(jiǎn)化這一點(diǎn)。我想根據(jù) BigDecimal 值設(shè)置 BackgroundTintList,在設(shè)置 TextView 元素的文本時(shí)我總是可以使用該值。
查看完整描述

2 回答

?
慕工程0101907

TA貢獻(xiàn)1887條經(jīng)驗(yàn) 獲得超5個(gè)贊

我認(rèn)為你有兩個(gè)選擇。


創(chuàng)建自定義 Utils.java 類


您可以創(chuàng)建一個(gè)自定義靜態(tài)類來為您更新文本視圖。


public class Utils {


    public static void setText(TextView textView, BigDecimal bigDecimal) {

        if(textView != null && bigDecimal != null) {

            // Get context

            Context context = textView.getContext();


            // Set text

            textView.setText(bigDecimal.toString());


            // Set color

            if (bigDecimal.compareTo(BigDecimal.ZERO) == -1) {

                textView.setBackgroundTintList(new ColorStateListStud().getList(ContextCompat.getColor(context, R.color.lightred)));

            } else if (bigDecimal.compareTo(BigDecimal.ZERO) == 1) {

                textView.setBackgroundTintList(new ColorStateListStud().getList(ContextCompat.getColor(context, R.color.lightgreen)));

            } else {

                textView.setBackgroundTintList(new ColorStateListStud().getList(ContextCompat.getColor(context, R.color.transparent)));

            }

        } else {

            Log.e("ERROR", "Error: TextView and/or BigDecimal is null");

        }

}

然后,你可以調(diào)用:


Utils.setText(mTextView, mBigDecimal);

創(chuàng)建您自己的自定義 TextView


public class CustomTextView extends TextView {


    public CustomTextView(final Context context) {

        this(context, null);

    }


    public CustomTextView(final Context context,

            @Nullable final AttributeSet attrs) {

        this(context, attrs, 0);

    }


    public CustomTextView(final Context context, @Nullable final AttributeSet attrs,

            final int defStyleAttr) {

        super(context, attrs, defStyleAttr);

    }


    public void setText(BigDecimal bigDecimal) {

        setText(bigDecimal.toString());


        // Set color

        if (bigDecimal.compareTo(BigDecimal.ZERO) == -1) {

            setBackgroundTintList(new ColorStateListStud().getList(ContextCompat.getColor(getContext(), R.color.lightred)));

        } else if (bigDecimal.compareTo(BigDecimal.ZERO) == 1) {

            setBackgroundTintList(new ColorStateListStud().getList(ContextCompat.getColor(getContext(), R.color.lightgreen)));

        } else {

            setBackgroundTintList(new ColorStateListStud().getList(ContextCompat.getColor(getContext(), R.color.transparent)));

        }

    }

}

然后,在java端:


CustomTextView mTextView = (CustomTextView) findViewById(R.id.text_view);

mTextView.setText(mBigDecimal);

在你的layout.xml中:


<com.test.CustomTextView

    android:id="@+id/text_view"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"/>


查看完整回答
反對(duì) 回復(fù) 2023-11-01
?
慕姐4208626

TA貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超7個(gè)贊

嘗試setText()在最后打電話


        if (mBigDecimal.compareTo(BigDecimal.ZERO) == -1) {

            payment.setBackgroundTintList(new ColorStateListStud().getList(ContextCompat.getColor(super.getContext(), R.color.lightred)));

        } else if (mBigDecimal.compareTo(BigDecimal.ZERO) == 1) {

            payment.setBackgroundTintList(new ColorStateListStud().getList(ContextCompat.getColor(super.getContext(), R.color.lightgreen)));

        } else {

            payment.setBackgroundTintList(new ColorStateListStud().getList(ContextCompat.getColor(super.getContext(), R.color.transparent)));

        }

        // Only then, call setText() here

        payment.setText(BigDecimal.toString());


查看完整回答
反對(duì) 回復(fù) 2023-11-01
  • 2 回答
  • 0 關(guān)注
  • 159 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)