Java 在调用setText时设置TextView的其他属性

Java 在调用setText时设置TextView的其他属性,java,android,Java,Android,我正在寻找一种简单的方法,根据TextView.setText设置的输入设置TextView元素的更多属性 具体来说,我的代码目前看起来如下: TextView payment; BigDecimal mBigDecimal; payment.setText(BigDecimal.toString()); if (mBigDecimal.compareTo(BigDecimal.ZERO) == -1) { payment.setBackgroundTintList(new Color

我正在寻找一种简单的方法,根据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元素的文本时,我总是可以使用BigDecimal值。

请在末尾尝试setText调用

如果mBigDecimal.compareToBigDecimal.ZERO==-1{ payment.setBackgroundTintListnew ColorStateListStud.getListContextCompat.getColorsuper.getContext,R.color.lightred; }如果mBigDecimal.compareToBigDecimal.ZERO==1,则为else{ payment.setBackgroundTintListnew ColorStateListStud.getListContextCompat.getColorsuper.getContext,R.color.lightgreen; }否则{ payment.setBackgroundTintListnew ColorStateListStud.getListContextCompat.getColorsuper.getContext,R.color.transparent; } //只有这样,才可以在这里调用setText payment.setTextBigDecimal.toString;
我想你有两个选择

创建自定义Utils.java类

您可以创建一个自定义静态类来更新文本视图

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");
        }
}
然后,你可以打电话:

Utils.setText(mTextView, mBigDecimal);
创建自己的自定义文本视图

然后,在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"/>

对不起,这不是我要问的问题,我的问题是我发布的代码在我的代码中重复了很多次。我想通过在调用setText时设置TextView元素的背景色来简化代码。感谢您创建CustomTextView类,当我尝试创建它时,android Studio告诉我该类应该扩展android.support.v7.widget.AppCompatTextView。但如果我这样做,然后尝试将TextView元素转换为我的CustomTextView类,Android Studio会抱怨出现意外转换。我可以忽略这个警告吗?它仍然在编译,所以我想这回答了我的问题。再次感谢。
<com.test.CustomTextView
    android:id="@+id/text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>