在安卓系统中如何拼接2个字符串?

6
我正在使用strings.xml在Toast中显示错误消息。像这样:mErrorMsgId=R.string.username_or_password_incorrectfull; 但现在我需要将消息与R.string.add_selfhosted_blog连接起来,以避免翻译不一致。阅读了一些类似的问题,但无法解决。 我想在username_or_password_incorrectfull字符串中的tap单词后面连接nux_add_selfhosted_blog。
<string name="username_or_password_incorrectfull">The username or password you entered is incorrect
 \- If you\'re a self hosted user, don\'t forget to tap **Add Self Hosted Site** and fill the URL field</string> 
添加自托管站点 我该如何实现这个功能?

展示更多的代码可以帮助我们更好地帮助您,字符串拼接实际上是一件非常简单的事情。 - Jorge Alfaro
请阅读编辑... - Arulnadhan
8个回答

5

由于它们不是String实例,而是指向strings.xml中实际字符串的资源ID,因此您不能直接将R.string.username_or_password_incorrectfullR.string.add_selfhosted_blog连接起来。 您可以获取这两个字符串,然后正常地进行连接。

像这样:

String string1 = getResources().getString(R.string.username_or_password_incorrectfull);
String string2 = getResources().getString(R.string.add_selfhosted_blog);

String combined = string1 + string2;

1
你可以使用MessageFormat。
将username_or_password_incorrectfull设置为格式字符串。
... 忘记点击 {0} 并填写URL ...
然后使用MessageFormat.format(username_or_password_incorrectfull, nux_add_selfhosted_blog)。

1

方法makeText的第二个参数接受stringResId或者String类型的参数。

例如:

String error = getResources().getString(R.string.username_or_password_incorrectful);
String error2 = getResources().getString(R.string.add_selfhosted_blog);
String conctString = error + " " + error2;
Toast.makeText(context, conctString, duration).show();

请阅读此链接:https://dev59.com/d3A65IYBdhLWcg3wvhXi - kstachniuk

0
如果您想要简单地连接来自strings.xml资源的这两个String,则可以通过以下方式实现...
String errorMessage = getString(R.string.username_or_password_incorrectfull) + getString(R.string.add_selfhosted_blog);

然后在“Toast”中显示连接的错误消息

Toast.makeText(context, errorMessage, Toast.LENGTH_SHORT).show();

1
操作问题中的 mErrorMsgId 必须是 int 类型。 - The Holy Coder

0
你可以通过以下方式在一个字符串中间插入另一个字符串,
String mErrorMsgId = getResources().getString(R.string.username_or_password_incorrectfull); // +  getResources().getString(R.string.nux_add_selfhosted_blog);

String firstPart = mErrorMsgId.substring( 0, mErrorMsgId.indexOf( "tap") + 4 );
String selfhosted_blog = getResources().getString(R.string.nux_add_selfhosted_blog) + " ";
String thirdPart = mErrorMsgId.substring( mErrorMsgId.indexOf( "tap") + 4 );
mErrorMsgId = firstPart + selfhosted_blog + thirdPart;

我已经添加了你在问题中提供的字符串,它向我展示了正确的输出,你有遇到任何错误吗? - Lucifer
你的代码只是简单地连接了两个字符串。但是我需要在一个字符串中间进行连接。 - Arulnadhan
你的意思是在这段文字后面加上“忘记点击”吗? - Lucifer
不可能的,我在我的Eclipse中运行了代码,并且它显示了正确的结果。 - Lucifer

0

只需从两个ID中检索字符串,像往常一样使用+连接它们,并将结果字符串变量放入Toast中。

String messageToUser = getResources().getString(R.string.username_or_password_incorrectfull) +  getResources().getString(R.string.add_selfhosted_blog);
Toast.makeText(context, messageToUser, Toast.LENGTH_SHORT).show();

0
你可以使用我创建的这个库来实现:https://github.com/LikeTheSalad/android-stem。它会基于你定义的模板字符串连接所有你想要的字符串,并在构建时生成最终的 XML 字符串。因此,对于你的情况,你可以像这样定义你的字符串:
<string name="nux_add_selfhosted_blog">Add self-hosted site</string>
<string name="username_or_password_incorrectfull">The username or password you entered is incorrect
 \- If you\'re a self hosted user, don\'t forget to tap ${nux_add_selfhosted_blog} and fill the URL field</string>

然后在构建项目之后,将生成以下字符串:

<!-- Auto generated during compilation -->
<string name="username_or_password_incorrectfull">The username or password you entered is incorrect
     \- If you\'re a self hosted user, don\'t forget to tap Add self-hosted site and fill the URL field</string>

该工具将自动生成的字符串与模板和值所做的任何更改保持同步。有关存储库页面的更多信息。


0

您可以像在C#/Java中一样简单地添加/连接字符串,

这里的Address1和Address2是EditText(文本框)

String finalString = Address1.getText().toString() + " "+ Address2.getText().toString();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接