【问题标题】:passing int to .xml for android app将 int 传递给 Android 应用程序的 .xml
【发布时间】:2012-03-19 03:19:14
【问题描述】:

我已尝试搜索,但找不到问题的答案。我试图简单地显示一个 int 的值。我想将它作为一个 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 android xml string


    【解决方案1】:

    使用 Java 的 Formatter 类格式化字符串语法实际上与您使用的语法不同,请尝试将“aaa”字符串更改为以下内容:

    <string name="aaa">hello %1$ </string>
    

    要了解发生了什么,您可以在此处阅读有关语法的更多信息:http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html#syntax

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多