android – 如何访问string.xml中的字符串

前端之家收集整理的这篇文章主要介绍了android – 如何访问string.xml中的字符串前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想用String.xml中的字符串构建alertdialog
但是R.string.string1返回int.请告诉我在string.xml中获取字符串prestent的方法
这是我的代码..
public static void showAlert(String alertMessage) {
    final AlertDialog.Builder builder = new AlertDialog.Builder(null);
    builder.setMessage(alertMessage);              
    final AlertDialog alert = builder.create();
    alert.show();
}

 showAlert(R.string.string1);

解决方法

使用getResources().getString从strings.xml获取字符串值:
String str=getResources().getString(R.string.internetNotAvailable);
showAlert(str);

并且您还将null传递给AlertDialog.Builder以创建AlertDialog.请传递当前的Activity上下文而不是null,以创建AlertDialog:

AlertDialog.Builder builder = new AlertDialog.Builder(Your_Current_Activity.this);

猜你在找的Android相关文章