3

我已经声明了一个String

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

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

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

但我想要的只是字符串的值Please

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

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

从您的所有答案中,我可以获得 app_name 字符串,但我是否可以使用当前日期设置此 app_name

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

我也要这样设置

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

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

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

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

但没有 setResource() 方法对不起。

4

5 回答 5

12

像这样试试

String temp = getResources().getString(R.string.Please);
于 2012-04-11T06:49:05.787 回答
8

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

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

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

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

这是该布局的 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>

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

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

然后设置你的内容视图

然后调用它来设置标题:

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

现在您可以找到两个 textView 并设置文本:

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);
于 2012-04-11T07:32:40.740 回答
5

试试这个。

String value = getResources().getString(R.string.Please);
于 2012-04-11T06:50:53.553 回答
2
 String str = getResources().getString(R.string.Please);
于 2012-04-11T06:49:22.837 回答
1

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

@string/Please
于 2012-04-11T06:48:38.060 回答