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

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

我写了这句话:

String Mess = R.string.mess_1 ;

获取字符串值,但它不是返回字符串,而是给我类型为整数的id。 我怎样才能得到它的字符串值? 我在string.xml文件中提到了字符串值。

16个解决方案

722 votes

试试这个

String mess = getResources().getString(R.string.mess_1);

UPDATE

String string = getString(R.string.hello);

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

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

ccheneson answered 2019-01-31T06:02:05Z

58 votes

在活动中:

this.getString(R.string.resource_name)

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

context.getString(R.string.resource_name)

application.getString(R.string.resource_name)

Hakan Cakirkan answered 2019-01-31T06:02:35Z

41 votes

我正在使用这个:

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

Robertas Uldukis answered 2019-01-31T06:02:53Z

21 votes

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

My Tab 1

My Tab 2

然后从您的Activity中获取引用,如下所示:

String[] tab_names = getResources().getStringArray(R.array.tab_names);

String tabname1=tab_names[0];//"My Tab 1"

oabarca answered 2019-01-31T06:03:22Z

10 votes

仅供将来参考。

在String资源文档中,它说:

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

Alvaro answered 2019-01-31T06:03:58Z

6 votes

解决方案1

Context context;

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

解决方案2

String mess = getString(R.string.mess_1)

thhVictor answered 2019-01-31T06:04:18Z

3 votes

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

android:text="@string/NameOfTheString"

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

Test string

bondus answered 2019-01-31T06:04:48Z

2 votes

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

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

要么

Context mcontext=getApplicationContext();

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

Ashish Vora answered 2019-01-31T06:05:15Z

2 votes

在片段中,您可以使用

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

ir2pid answered 2019-01-31T06:05:37Z

1 votes

您可以使用此代码:

getText(R.string.mess_1);

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

coderz answered 2019-01-31T06:06:01Z

1 votes

如果您正在参加活动,则可以使用

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

如果您不在活动中用这个 :

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

Hoque MD Zahidul answered 2019-01-31T06:06:31Z

0 votes

**

我希望这段代码是有益的

**

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

dhiraj kakran answered 2019-01-31T06:06:58Z

0 votes

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

Giridhar Karnik answered 2019-01-31T06:07:21Z

0 votes

更新

您可以在manifest.xml或manifest.xml中使用manifest.xml。

您可以使用manifest.xml,但您无法直接访问

问题是您没有manifest.xml访问权限,就像manifest.xml类中的方法一样。

假设没有上下文的方法如下。

public void someMethod(){

...

// can't use getResource() or getString() without Context.

}

现在您将通过manifest.xml作为此方法中的参数并使用

public void someMethod(Context context){

...

context.getString(R.string.some_id);

}

我做的是

public void someMethod(){

...

App.getRes().getString(R.string.some_id)

}

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

所以这里有一个Bonus独特的解决方案,您可以通过它来访问manifest.xml等任何地方的资源。

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

android:name=".App"

...

>

...

现在你很高兴。

Khemraj answered 2019-01-31T06:08:44Z

0 votes

细节

Android Studio 3.1.4

Kotlin版本:1.2.60

任务

单线使用

最小代码

使用编译器的建议

第1步。申请()

获取指向您的应用程序上下文的链接

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)

用法

strings.xml中

No internet connection

获取字符串值:

val errorMessage = R.string.no_internet_connection.toLocalizedString()

结果

3b16834429fd8433861943c0832173c9.png

c292eeb9d8f2b5107cd0d4f19aac7c2e.png

Vasily Bodnarchuk answered 2019-01-31T06:10:20Z

0 votes

getString(R.string.your_string)得到结果

Bisht Lalit answered 2019-01-31T06:10:43Z

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值