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

我可以在堆栈溢出的帮助下设法生成自定义 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);

}


@Override

public 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



    }

}}

如何摆脱这个?


开满天机
浏览 52回答 1
1回答

吃鸡游戏

如果要显示数值并在末尾添加美元符号,首先定义一个格式字符串资源,如下所示:<string name="amount">%1$s $</string>然后提供如下数值:double amount = "100.00"...amountTextView.setText(context.getString(R.string.amount, amount));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java