Android中Strings.xml使用占位符示例

MainActivity如下:

package cn.test;
import android.os.Bundle;
import android.app.Activity;
/**
 * Demo描述:
 * 在资源文件Strings.xml中的某个string里使用占位符
 * 然后在代码中将其替换
 */
public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		init();
	}
    private void init(){
    	String stringStart=getResources().getString(R.string.stringStart);
    	String startResult=String.format(stringStart, "占位符的测试:");
    	System.out.println("startResult="+startResult);

    	String stringTest=getResources().getString(R.string.stringTest);
    	String stringResult=String.format(stringTest, "周星驰","香港",55);
    	System.out.println("stringResult="+stringResult);

    	String numberTest=getResources().getString(R.string.numberData);
    	String numberResult=String.format(numberTest,9527,88.88f);
    	System.out.println("numberResult="+numberResult);

    }

}

Strings.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">TestStringResource</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>

    <string name="stringStart">现在开始%1$s</string>

    <string name="stringTest">My name is %1$s , I am from %2$s,I am %3$d years old</string>

    <!-- .3f表示的是保留三位小数的浮点数  -->
    <string name="numberData">我的编号是(整数型):%1$d , 我的工资是(浮点型):%2$.3f</string>

</resources>

 

main.xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="25sp"
        android:text="@string/hello_world"
     />

</RelativeLayout>

 

时间: 2024-08-02 07:54:26

Android中Strings.xml使用占位符示例的相关文章

【我的Android进阶之旅】解决strings.xml格式化占位符错误: Multiple substitutions specified in non-positional format

今天有一个Android新手使用strings.xml进行格式化的时候报了占位符错误, Multiple substitutions specified in non-positional format; did you mean to add the formatted="false" attribute? ,问我该如何解决? 一.错误描述 具体错误描述如下所示: D:\Code_For_Android_Studio\MyGame2048\app\build\intermediate

Android中属性动画Property Animation使用示例(一)

MainActivity如下: package cc.cn; import android.animation.Animator; import android.animation.AnimatorInflater; import android.animation.AnimatorListenerAdapter; import android.animation.AnimatorSet; import android.animation.ObjectAnimator; import andro

Android中Java反射技术的使用示例

MainActivity如下: package cn.testreflect; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; import android.os.Bundle; import android.app.Activity; /** * Demo描述: * Android中Java反射技术的使用示例 * 在Java中描述字节码文

布局-Android中不同xml文件中id可以重复么?

问题描述 Android中不同xml文件中id可以重复么? Android中如果两个layout布局文件中都有一个TextView控件,而且拥有同样的id,那样的话会乱套嘛? 比如我有a.xml,中间有控件TextView id为tv,有b.xml,中间有控件TextView id为tv,在R文件中能看到id内部类中只有一个tv常量,调用时是因为用之前是用色图ContentView设置了对应的布局所以不会混乱嘛,还是说就是会混乱的呢,我记得以前好像用的时候用混乱过,但是今天听老师讲课说是可行的,

xml-如何解析 android 中的 XML?

问题描述 如何解析 android 中的 XML? 我需要把下面的 xml 解析 为输出: String id="3" String name="str1" String path="/mnt/sdcard/path2" String type="2" String desc="des3)" 在 android中如何实现呢? <xmldump> <mfs id="3"

接口-android中的xml里实现onClick=“clickButton” 在java中……

问题描述 android中的xml里实现onClick="clickButton" 在java中-- 1.对Android中的xml实现onClick有点疑问,实现的原理是怎样的? 2.java中实现是这样的: view.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { } }); 3.xml结合java的实现样式比较好看,可是他没有实现OnClickListener的接

Android 中Manifest.xml文件详解

Android 中Manifest.xml文件详解 每一个Android项目都包含一个清单(Manifest)文件--AndroidManifest.xml,它存储在项目层次中的最底层.清单可以定义应用程序及其组件的结构和元数据. 它包含了组成应用程序的每一个组件(活动.服务.内容提供器和广播接收器)的节点,并使用Intent过滤器和权限来确定这些组件之间以及这些组件和其他应用程序是如何交互的. 它还提供了各种属性来详细地说明应用程序的元数据(如它的图标或者主题)以及额外的可用来进行安全设置和单

Android中js和原生交互的示例代码

本文介绍了Android中js和原生交互的示例代码,分享给大家,具体如下: 加载webview的类 public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); JavaScriptInterf

Android中Intent机制详解及示例总结(总结篇)_Android

最近在进行android开发过程中,在将 Intent传递给调用的组件并完成组件的调用时遇到点困难,并且之前对Intent的学习也是一知半解,最近特意为此拿出一些时间,对Intent部分进行了系统的学习并进行了部分实践,下面将自己的学习及Intent知识进行了详细的归纳整理,希望能帮助到同样遇到相同问题的博友. 下面是Intent介绍.详解及Intent示例总结: 一.Intent介绍: Intent的中文意思是"意图,意向",在Android中提供了Intent机制来协助应用间的交互