【发布时间】:2011-01-12 03:23:34
【问题描述】:
我已经写了一行:
String Mess = R.string.mess_1 ;
获取字符串值,但不是返回字符串,而是给我整数类型的 id。我怎样才能得到它的字符串值?我在string.xml文件中提到了字符串值。
【问题讨论】:
我已经写了一行:
String Mess = R.string.mess_1 ;
获取字符串值,但不是返回字符串,而是给我整数类型的 id。我怎样才能得到它的字符串值?我在string.xml文件中提到了字符串值。
【问题讨论】:
试试这个
String mess = getResources().getString(R.string.mess_1);
更新
String string = getString(R.string.hello);
您可以使用getString(int) 或getText(int) 来检索字符串。 getText(int) 将保留应用于字符串的任何富文本样式。
参考:https://developer.android.com/guide/topics/resources/string-resource.html
【讨论】:
Context(活动或服务)中,则可以将其简化为 this.getString(R.string.some_id)。
getResouces().getInteger(R.integer.my_value_in_xml)
仅供日后参考。
在String resources documentation 中写道:
您可以使用 getString(int) 或 getText(int) 来检索字符串。 getText(int) 将>保留应用于字符串的任何富文本样式。
【讨论】:
活动中:
this.getString(R.string.resource_name)
如果不在活动中但可以访问上下文:
context.getString(R.string.resource_name)
application.getString(R.string.resource_name)
【讨论】:
this。只需getString() 即可让您做事。
解决方案 1
Context context;
String mess = context.getString(R.string.mess_1)
解决方案 2
String mess = getString(R.string.mess_1)
【讨论】:
我正在使用这个:
String URL = Resources.getSystem().getString(R.string.mess_1);
【讨论】:
Resources.getSystem() 不会给你应用程序资源,而只会给你android 的资源。它不应该用于像string 这样的资源。我使用了这个解决方案,应用程序崩溃了,抛出了一个难以理解的notfoundexception(因为资源存在于strings.xml中)。
**
希望这段代码有用
**
String user = getResources().getString(R.string.muser);
【讨论】:
顺便说一句,也可以像这样在strings.xml中创建字符串数组:
<string-array name="tabs_names">
<item>My Tab 1</item>
<item>My Tab 2</item>
</string-array>
然后从您的活动中,您可以像这样获得参考:
String[] tab_names = getResources().getStringArray(R.array.tab_names);
String tabname1=tab_names[0];//"My Tab 1"
【讨论】:
当你写R时。您指的是 eclipse 创建的 R.java 类,使用 getResources().getString() 并在 getString() 方法中传递您尝试从中读取的资源的 id。
示例: String[] yourStringArray = getResources().getStringArray(R.array.Your_array);
【讨论】:
在 Android 中使用 getResources() 之前必须引用上下文名称。
String user=getApplicationContext().getResources().getString(R.string.muser);
或
Context mcontext=getApplicationContext();
String user=mcontext.getResources().getString(R.string.muser);
【讨论】:
您可以使用此代码:
getText(R.string.mess_1);
基本上,您需要将资源 id 作为参数传递给 getText() 方法。
【讨论】:
在片段中,可以使用
getActivity().getString(R.id.whatever);
【讨论】:
如果你想给一个按钮添加字符串值,例如,简单的使用
android:text="@string/NameOfTheString"
strings.xml 中定义的文本如下所示:
<string name="NameOfTheString">Test string</string>
【讨论】:
如果你在一个活动中你可以使用
getResources().getString(R.string.whatever_string_youWant);
如果您不在活动中 使用这个:
getApplicationContext.getResource().getString(R.String.Whatever_String_you_want)
【讨论】:
更新
Activity 或 Fragment 中使用 getString(R.string.some_string_id)。getString() 方法的情况下使用Context.getString(R.string.some_string_id)。喜欢Dialog。问题是您没有Context 访问权限,就像您的Util 类中的方法一样。
假设下面的方法没有上下文。
public void someMethod(){
...
// can't use getResource() or getString() without Context.
}
现在您将在此方法中将Context 作为参数传递并使用getString().
public void someMethod(Context context){
...
context.getString(R.string.some_id);
}
我做的是
public void someMethod(){
...
App.getRes().getString(R.string.some_id)
}
什么?在您的应用中的任何位置使用都非常简单!
所以这里有一个独特的解决方案,您可以通过它从任何地方访问资源,例如Util class。
import android.app.Application;
import android.content.res.Resources;
public class App extends Application {
private static App mInstance;
private static Resources res;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
res = getResources();
}
public static App getInstance() {
return mInstance;
}
public static Resources getResourses() {
return res;
}
}
将名称字段添加到您的 manifest.xml <application 标记中。
<application
android:name=".App"
...
>
...
</application>
现在你可以走了。
【讨论】:
获取应用程序上下文的链接
class MY_APPLICATION_NAME: Application() {
companion object {
private lateinit var instance: MY_APPLICATION_NAME
fun getAppContext(): Context = instance.applicationContext
}
override fun onCreate() {
instance = this
super.onCreate()
}
}
inline fun Int.toLocalizedString(): String = MY_APPLICATION_NAME.getAppContext().resources.getString(this)
字符串.xml
<resources>
<!-- ....... -->
<string name="no_internet_connection">No internet connection</string>
<!-- ....... -->
</resources>
获取字符串值:
val errorMessage = R.string.no_internet_connection.toLocalizedString()
【讨论】:
getString(R.string.your_string) 得到结果
【讨论】:
String myString = getResources().getString(R.string.here_your_string_name);
现在您的字符串被复制到 myString 中。我希望它对你有用。
【讨论】:
可以直接读取strings.xml中定义的值:
<resources>
<string name="hello">Hello StackOverflow!</string>
</resources>
并设置成一个变量:
String mymessage = getString(R.string.hello);
但我们可以将字符串定义到视图中:
<TextView
android:id="@+id/myTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello"/>
【讨论】: