我已尝试搜索,但找不到问题的答案。我试图简单地显示一个 int 的值。我想把它作为一个整数传递。这是我的代码。由于某种原因,它没有显示实际数字,而是显示 %d。
java代码:
package ebike.picture.com;
import android.app.Activity;
import android.os.Bundle;
public class PictureActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String hello = getString(R.string.aaa, 10);
}
}
来自 strings.xml 的 xml 代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, PictureActivity!</string>
<string name="app_name">Picture</string>
<string name="aaa">hello %d </string>
</resources>
来自 main.xml 的 xml 代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/aaa" />
</LinearLayout>
提前致谢!
最佳答案
使用 Java 的 Formatter
类格式化字符串语法实际上与您使用的语法不同,尝试将“aaa”字符串更改为以下内容:
<string name="aaa">hello %1$ </string>
要了解发生了什么,您可以在此处阅读有关语法的更多信息:http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html#syntax
关于java - 将 int 传递给 Android 应用程序的 .xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9764590/