为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 customtextview 中连接可变文本值和固定字符串

如何在 customtextview 中连接可变文本值和固定字符串

开满天机 2023-02-23 16:38:18
我可以在堆栈溢出的帮助下设法生成自定义 TextView,现在我很想将可变文本与固定字符串连接起来。我已经这样做了,但没有帮助。import android.content.Context;导入 android.support.annotation.Nullable;导入 android.util.AttributeSet;公共类 CustomTextViewTest 扩展 android.support.v7.widget.AppCompatTextView {public CustomTextViewTest(Context context) {    super(context);}public CustomTextViewTest(Context context, @Nullable AttributeSet attrs) {    super(context, attrs);}public CustomTextViewTest(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {    super(context, attrs, defStyleAttr);}@Overridepublic void setText(CharSequence text, BufferType type) {    super.setText(text, type);    // here set color according to text    if (text.toString().contains("Available")) {        this.setTextColor(getResources().getColor(R.color.green));    }    if(text.toString().contains("Not Available")){        this.setTextColor(getResources().getColor(R.color.red)); }    if(text.toString().startsWith("0")||text.toString().startsWith("1")||text.toString().startsWith("2")||text.toString().startsWith("3")||text.toString().startsWith("4")||text.toString().startsWith("5")||            text.toString().startsWith("6")||text.toString().startsWith("7")||text.toString().startsWith("8")            ||text.toString().startsWith("9")){        setText(text.toString()+" $");  //this line show something unexpected    }}}如何摆脱这个?
查看完整描述

1 回答

?
吃鸡游戏

TA贡献1829条经验 获得超7个赞

如果要显示数值并在末尾添加美元符号,首先定义一个格式字符串资源,如下所示:


<string name="amount">%1$s $</string>

然后提供如下数值:


double amount = "100.00"

...

amountTextView.setText(context.getString(R.string.amount, amount));


查看完整回答
反对 回复 2023-02-23
  • 1 回答
  • 0 关注
  • 51 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信