【Android】AS警告解決方法:String literal in setText can not be translated. Use Android resources instead.

轉載請註明出處,原文鏈接:https://blog.csdn.net/u013642500/article/details/80166941

【錯誤】

String literal in setText can not be translated. Use Android resources instead.

【翻譯】

在setText方法中的字符串文字不能被轉換。使用Android資源代替之。

【造成原因】

在TextView對象引用setText方法時,直接傳入字符串。

【舉例】

        TextView textView = new TextView(this);
        textView.setText("text");    // 此處報錯:String literal in setText can not be translated. Use Android resources instead.

【解決方法】

1、在res文件夾中的values文件夾中的strings.xml文件中添加字符串。

<resources>
    <string name="txtText">text</string>
</resources>

2、在TextView對象引用setText方法時,傳入getString方法。

        TextView textView = new TextView(this);
        textView.setText(getString(R.string.txtText));

【相關警告】

錯誤:Do not concatenate text displayed with setText. Use resource string with placeholders.

詳見:https://blog.csdn.net/u013642500/article/details/80167402

【說明】

本文可能未必適用所有情形,本人尚屬初學者,如有錯誤或疑問請評論提出,由衷感謝!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章