如何在android中获取和设置字符串资源值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10101174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:29:27  来源:igfitidea点击:

How to get and set string Resource values in android

androidstringandroid-resources

提问by Trikaldarshi

I've declare a String:

我已经声明了一个String

<string name="Please">Please Select</string>

in the res/values/Strings.xml. Now I want to access this Stringvalue but what I can get can go till id not value like:

res/values/Strings.xml. 现在我想访问这个String值,但我能得到的可以一直持续到 id 不值,例如:

 int i = findViewById(R.string.Please);

But what I want is just the value of the string Please.

但我想要的只是 string 的值Please

Thanks for all of your answer but my problem is a little bigger

感谢您的所有回答,但我的问题有点大

What I wanted to do is just to display date with app_name

我想要做的只是用 app_name 显示日期

from all your answers i can get the app_name String but whether I can set This app_name with current date

从你所有的答案中,我可以得到 app_name 字符串,但是我是否可以将这个 app_name 设置为当前日期

<string name="app_name">My app</string>

and also I'm to set my in this way

而且我要以这种方式设置我的

My app(in Left alignment)             Date (in right aligment)

and also on start of first activity i want to set app_name and not on every activity;

并且在第一个活动开始时,我想设置 app_name 而不是每个活动;

I think it may work let me try

我认为它可能有效让我试试

setResource().setString(R.string.Please);

but no setResource() method sorry.

但没有 setResource() 方法抱歉。

回答by Bhavin

Try like this

像这样尝试

String temp = getResources().getString(R.string.Please);

回答by FabianCook

You cant set a string, its static, and cant be changed at run-time, what you want to do is something like this

你不能设置一个字符串,它是静态的,不能在运行时改变,你想做的是这样的

        Calendar cal = Calendar.getInstance();
        String date = cal.get(Calendar.DAY_OF_MONTH) + "/" + cal.get(Calendar.MONTH) + "/" + cal.get(Calendar.YEAR);
        setTitle(date);

Your trying to set the title as the date right?

您试图将标题设置为日期吗?

To have two titles it is a bit harder, first you need a layout, call this custom_title_1

有两个标题有点难,首先你需要一个布局,称之为custom_title_1

This is the xml code for that layout

这是该布局的 xml 代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/screen"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView android:id="@+id/left_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="@string/custom_title_left" />
    <TextView android:id="@+id/right_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="@string/custom_title_right" />
</RelativeLayout>

Then in your code before you call the UI you need to call this:

然后在调用 UI 之前的代码中,您需要调用它:

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

then set you content view

然后设置你的内容视图

then call this which will set the title:

然后调用这将设置标题:

getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_1);

Now you can find the two textViews and set the text:

现在你可以找到两个 textViews 并设置文本:

TextView leftText = (TextView) findViewById(R.id.left_text);
TextView rightText = (TextView) findViewById(R.id.right_text);

Calendar cal = Calendar.getInstance();
String date = cal.get(Calendar.DAY_OF_MONTH) + "/" + cal.get(Calendar.MONTH) + "/" + cal.get(Calendar.YEAR);
String app_name = getString(R.string.app_name);
leftText.setText(app_name);
rightText.setText(date);

回答by android developer

Try this.

尝试这个。

String value = getResources().getString(R.string.Please);

回答by Mohammed Azharuddin Shaikh

 String str = getResources().getString(R.string.Please);

回答by Amit

String resources are not views. To use a string resource you would do

字符串资源不是视图。要使用字符串资源,你会这样做

@string/Please