调用 setText 时设置 TextView 的更多属性

我正在寻找一种简单的方法来根据 TextView.setText() 设置的输入设置 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

这在我的整个代码中被复制了多次。所以我想尽可能地简化这一点。我想根据 BigDecimal 值设置 BackgroundTintList,在设置 TextView 元素的文本时我总是可以使用该值。


守着一只汪
浏览 114回答 2
2回答

慕工程0101907

我认为你有两个选择。创建自定义 Utils.java 类您可以创建一个自定义静态类来为您更新文本视图。public class Utils {&nbsp; &nbsp; public static void setText(TextView textView, BigDecimal bigDecimal) {&nbsp; &nbsp; &nbsp; &nbsp; if(textView != null && bigDecimal != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Get context&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Context context = textView.getContext();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Set text&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; textView.setText(bigDecimal.toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Set color&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (bigDecimal.compareTo(BigDecimal.ZERO) == -1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; textView.setBackgroundTintList(new ColorStateListStud().getList(ContextCompat.getColor(context, R.color.lightred)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (bigDecimal.compareTo(BigDecimal.ZERO) == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; textView.setBackgroundTintList(new ColorStateListStud().getList(ContextCompat.getColor(context, R.color.lightgreen)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; textView.setBackgroundTintList(new ColorStateListStud().getList(ContextCompat.getColor(context, R.color.transparent)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.e("ERROR", "Error: TextView and/or BigDecimal is null");&nbsp; &nbsp; &nbsp; &nbsp; }}然后,你可以调用:Utils.setText(mTextView, mBigDecimal);创建您自己的自定义 TextViewpublic class CustomTextView extends TextView {&nbsp; &nbsp; public CustomTextView(final Context context) {&nbsp; &nbsp; &nbsp; &nbsp; this(context, null);&nbsp; &nbsp; }&nbsp; &nbsp; public CustomTextView(final Context context,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Nullable final AttributeSet attrs) {&nbsp; &nbsp; &nbsp; &nbsp; this(context, attrs, 0);&nbsp; &nbsp; }&nbsp; &nbsp; public CustomTextView(final Context context, @Nullable final AttributeSet attrs,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final int defStyleAttr) {&nbsp; &nbsp; &nbsp; &nbsp; super(context, attrs, defStyleAttr);&nbsp; &nbsp; }&nbsp; &nbsp; public void setText(BigDecimal bigDecimal) {&nbsp; &nbsp; &nbsp; &nbsp; setText(bigDecimal.toString());&nbsp; &nbsp; &nbsp; &nbsp; // Set color&nbsp; &nbsp; &nbsp; &nbsp; if (bigDecimal.compareTo(BigDecimal.ZERO) == -1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setBackgroundTintList(new ColorStateListStud().getList(ContextCompat.getColor(getContext(), R.color.lightred)));&nbsp; &nbsp; &nbsp; &nbsp; } else if (bigDecimal.compareTo(BigDecimal.ZERO) == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setBackgroundTintList(new ColorStateListStud().getList(ContextCompat.getColor(getContext(), R.color.lightgreen)));&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setBackgroundTintList(new ColorStateListStud().getList(ContextCompat.getColor(getContext(), R.color.transparent)));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}然后,在java端:CustomTextView mTextView = (CustomTextView) findViewById(R.id.text_view);mTextView.setText(mBigDecimal);在你的layout.xml中:<com.test.CustomTextView&nbsp; &nbsp; android:id="@+id/text_view"&nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; android:layout_height="wrap_content"/>

慕姐4208626

尝试setText()在最后打电话&nbsp; &nbsp; &nbsp; &nbsp; if (mBigDecimal.compareTo(BigDecimal.ZERO) == -1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; payment.setBackgroundTintList(new ColorStateListStud().getList(ContextCompat.getColor(super.getContext(), R.color.lightred)));&nbsp; &nbsp; &nbsp; &nbsp; } else if (mBigDecimal.compareTo(BigDecimal.ZERO) == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; payment.setBackgroundTintList(new ColorStateListStud().getList(ContextCompat.getColor(super.getContext(), R.color.lightgreen)));&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; payment.setBackgroundTintList(new ColorStateListStud().getList(ContextCompat.getColor(super.getContext(), R.color.transparent)));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // Only then, call setText() here&nbsp; &nbsp; &nbsp; &nbsp; payment.setText(BigDecimal.toString());
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java