繁体   English   中英

如何从android中的string.xml读取值?

[英]how to read value from string.xml in android?

我写了一行:

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)将保留应用于字符串的getText(int)文本样式。

参考: https : //developer.android.com/guide/topics/resources/string-resource.html

在活动中:

this.getString(R.string.resource_name)

如果不在活动中但可以访问上下文:

context.getString(R.string.resource_name)
application.getString(R.string.resource_name)

我正在使用这个:

String URL = Resources.getSystem().getString(R.string.mess_1);

顺便说一句,也可以在 strings.xml 中创建字符串数组,如下所示:

<string-array name="tabs_names"> 
    <item>My Tab 1</item> 
    <item>My Tab 2</item>
</string-array>

然后从你的 Activity 你可以得到这样的参考:

String[] tab_names = getResources().getStringArray(R.array.tab_names);
String tabname1=tab_names[0];//"My Tab 1"

仅供日后参考。

字符串资源文档中,它说:

您可以使用 getString(int) 或 getText(int) 来检索字符串。 getText(int) 将 > 保留应用于字符串的任何富文本样式。

解决方案1

Context context;
String mess = context.getString(R.string.mess_1)

解决方案2

String mess = getString(R.string.mess_1)

在片段中,您可以使用

getActivity().getString(R.id.whatever);

例如,如果要将字符串值添加到按钮,则简单使用

android:text="@string/NameOfTheString"

strings.xml 中定义的文本如下所示:

 <string name="NameOfTheString">Test string</string>

在 Android 中使用getResources()之前,您必须引用上下文名称。

String user=getApplicationContext().getResources().getString(R.string.muser);

或者

Context mcontext=getApplicationContext();

String user=mcontext.getResources().getString(R.string.muser);

当你写R 您指的是由 eclipse 创建的R.java类,使用getResources().getString()并在getString()方法中传递您试图从中读取的资源的id

示例: String[] yourStringArray = getResources().getStringArray(R.array.Your_array);

您可以使用此代码:

getText(R.string.mess_1);

基本上,您需要将资源 ID 作为参数传递给 getText() 方法。

如果你在一个活动中,你可以使用

getResources().getString(R.string.whatever_string_youWant);

如果您不在活动中,请使用以下命令:

getApplicationContext.getResource().getString(R.String.Whatever_String_you_want)

细节

  • 安卓工作室 3.1.4
  • 科特林版本:1.2.60

任务

  • 单线使用
  • 最小代码
  • 使用编译器的建议

步骤 1. 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()
    }

}

步骤 2. 添加 int 扩展名

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()

结果

在此处输入图片说明 在此处输入图片说明

**

我希望这段代码是有益的

**

String user = getResources().getString(R.string.muser); 

更新

  • 您可以在ActivityFragment使用getString(R.string.some_string_id)
  • 您可以在无法直接访问getString()方法的情况下使用Context.getString(R.string.some_string_id) Dialog

问题您没有Context access ,就像您的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)
}

什么? 在您的应用程序中的任何地方使用都非常简单!

所以这是一个Bonus 独特的解决方案,您可以通过它从任何地方访问资源,比如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>

现在你可以走了。

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"/>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM