作者:mobiledu2502912781 | 来源:互联网 | 2022-12-08 13:44
我尝试将大字符串(25k +个字符)设置为a TextView
已有一段时间,但是我一直在获取“ STRING_TOO_LARGE”输出。
我在某处读到在运行时设置字符串可能有助于解决该问题,因此我修改了代码,但这似乎无济于事。我还尝试直接在其中设置资源ID,setText()
但这并没有做任何事情。
我使用以下方法Activity
显示terms_dialog
:
private void showTermsPopup() {
Dialog termsDialog = new Dialog(this);
termsDialog.setContentView(R.layout.terms_dialog);
termsDialog.getWindow().setLayout(WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT);
// Cancel the dialog if you touch the background
termsDialog.setCanceledOnTouchOutside(true);
// Add the terms string to the dialog
TextView termsText = termsDialog.findViewById(R.id.termsTextView);
String terms = getResources().getString(R.string.terms);
termsText.setText(terms);
// Show the dialog
termsDialog.show();
}
这是terms_dialog.xml
文件:
而这是我得到的结果(敏感信息粉饰出来)。
1> nikolouzos..:
如链接所指出的那样@HB。给了我,您的APK文件中的字符串不能超过32,767字节(以UTF-8编码)。
因此,我要做的是在名为Assets的文件夹中创建一个txt文件,terms.txt
然后将字符串放入其中。然后,使用以下函数将文件转换为字符串:
private String getTermsString() {
StringBuilder termsString = new StringBuilder();
BufferedReader reader;
try {
reader = new BufferedReader(
new InputStreamReader(getAssets().open("terms.txt")));
String str;
while ((str = reader.readLine()) != null) {
termsString.append(str);
}
reader.close();
return termsString.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
而我只是将其分配给TextView
。