繁体   English   中英

调用 setText 时设置 TextView 的进一步属性

[英]Set further attributes of TextView when setText is called

我正在寻找一种简单的方法来根据 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 元素的文本时,我总是在手边。

我认为你有两个选择。

创建自定义 Utils.java class

您可以创建一个自定义 static class 来为您更新文本视图。

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);

创建您自己的自定义 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"/>

最后尝试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());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM