1. 程式人生 > >【Android】AS警告解決方法:String literal in setText can not be translated. Use Android resources instead.

【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

【說明】

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