gpt4 book ai didi

java - 如何让动态字符串出现在我的 View 中?

转载 作者:行者123 更新时间:2023-12-02 05:27:00 25 4
gpt4 key购买 nike

例如,我想显示我的设备上次与另一台设备同步的日期时间/字符串。我现在有这样的事情:

        Resources resources = context.getResources();
String syncString = String.format(resources.getString(R.string.last_sync), fileLastSync);

我将资源字符串保存在我的values/strings.xml中:

<string name="last_sync">Last synced  %s</string>

在我的 Activity View 中,我有以下内容:

 <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="@string/last_sync"
/>

但是当我运行我的设备时,只出现字符串符号。

我做错了什么?它应该显示日期时间字符串而不是占位符。

例如,我对字符串进行了硬编码:

String fileLastSync = "09-18-2014";

我的输出:

上次同步:%s

我的预期输出:

上次同步:2014 年 9 月 18 日

最佳答案

这里,

<TextView
android:id="@+id/last_sync"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="@string/last_sync"
/>

然后在onCreate()中输入以下内容:

Resources resources = context.getResources();
String syncString = String.format(resources.getString(R.string.last_sync), fileLastSync);

TextView lastSyncTextView = ( (TextView) findViewById(R.id.last_sync) );

lastSyncTextView.setText(syncString);

关于java - 如何让动态字符串出现在我的 View 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25919088/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com