java - 将 int 传递给 Android 应用程序的 .xml

标签 java android xml string

我已尝试搜索,但找不到问题的答案。我试图简单地显示一个 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/

相关文章:

xml - 如何解决此错误 "The element type "head"must be terminated > by the matching end-tag "</head>"?

java - 正确使用 Singleton getInstance 方法

iphone - 适用于 Android 和 iPhone 的 OpenGL

android - MediaStore 从特定路径列出

android - ffmpeg 的 ICY 元数据支持

javascript - XML/XSL 和 Javascript 购物车

c# - Json.Net 将复杂对象序列化为 Xml 属性和值

java - (JAVA) 添加新组件时如何实现滚动?

Java 服务器返回 HTTP 响应代码 : 401

java - 加载实体而不锁定数据库中的行