可能重复作为底部链接。对于我要提出的特定问题或想获得更快的答案,请参阅问题中的答案。否则,请转到以下链接。
================================================== =========================我的问题:
我问这个问题,即使可能有重复项,但对我没有用。
我正在尝试访问
strings.xml
在AlertDialog中,但我似乎做不到。我说过
.setTitle(@ string / AlertTitle)
在下面的代码中,但没有成功,并给出了一个错误。有没有可能从AlertDialog获取字符串的方法,还是不可能?
下面的代码。
这是AlertMainActivity.java
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Add a New Task") //AlertTitle
.setMessage("What do you want to do next?") //AlertMessage
.setView(taskEditText)
.setPositiveButton("Add", new DialogInterface.OnClickListener() //Add {
...
.setNegativeButton("Cancel", null) //Cancel
...
}
Run Code Online (Sandbox Code Playgroud)
和strings.xml
<string name="AlertTitle">Add a New Task</string>
<string name="AlertMessage">What do you want to do next?</string>
<string name="Add">Add</string>
<string name="Cancel">Cancel</string>
Run Code Online (Sandbox Code Playgroud)
谢谢!
以编程方式使用String资源时,应使用
setTitle(getString(R.string.AlertTitle));
Run Code Online (Sandbox Code Playgroud)
代替
.setTitle(@string/AlertTitle);
Run Code Online (Sandbox Code Playgroud)
同样对于这种特殊情况,有两种设置标题的方法。通过资源ID,这将是R.string.AlertTitle或通过设置一个字符串,可以从getString()
.setTitle(R.string.AlertTitle);
Run Code Online (Sandbox Code Playgroud)
参见Android:如何使用资源名称从资源中获取字符串?想要查询更多的信息。您可以getString代替getResources().getString(...)在Activity或中使用它Fragment
| 归档时间: |
|
| 查看次数: |
1920 次 |
| 最近记录: |