我可以在堆棧溢出的幫助下設法生成自定義 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));
添加回答
舉報
0/150
提交
取消