我想比较我的结果(字符串),我正在使用以下代码来检查它。
result = "password";
if (result.equals(R.string.myResponse)) {
//do something
}
R.字符串文件
<string name="myResponse">password</string>
以上功能无法正常工作,无法进入if
部分。
但是,如果我替换 R.string.myResponse
其实际值是 if(result.equals("password"))
然后就可以正常工作了。
使用 R.string
中的字符串值有什么问题?
最佳答案
R.string.myResponse 不是字符串,它是字符串的 ID
。
您需要使用该ID
获取字符串。
类似context.getString(R.string.myResponse)
,或者如果您处于 Activity 或 fragment 中,则只需getString(R.string.myResponse)
。
关于java - String.equal() 函数无法处理 `R.string` 中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38351539/