Android 代码中使用字符串方法。
2016-04-12 16:30
423 查看
在Android中要使用字符串一般要用到Resources。使用它的getString方法即可
通常我们如下定义:
Resources mResources;
mResources = getResources();
然后调用mResources.getString(R.string.xxx);
格式化的时候使用String.format(mResources.getString(R.string.xxx),args); 在写这个文章前,我也是这么使用的。
但是今天看到了一个代码,真的是自惭形秽。如此常用的功能Android 怎么会没有自己写好的方法呢。代码是这么写的:
跟进了下发现其实此方法就在Context类里,就在Context类里,就在Context类里,此时我是崩溃的。如此基础的类却不知道还存在这么个方法。
两个方法,一个是直接获取字符串,一个是带参数的字符串。
哎,还有好多好学西的,加油!
通常我们如下定义:
Resources mResources;
mResources = getResources();
然后调用mResources.getString(R.string.xxx);
格式化的时候使用String.format(mResources.getString(R.string.xxx),args); 在写这个文章前,我也是这么使用的。
但是今天看到了一个代码,真的是自惭形秽。如此常用的功能Android 怎么会没有自己写好的方法呢。代码是这么写的:
mTextView.setText(getString(R.string.text, 0));
跟进了下发现其实此方法就在Context类里,就在Context类里,就在Context类里,此时我是崩溃的。如此基础的类却不知道还存在这么个方法。
/** * Returns a localized string from the application's package's * default string table. * * @param resId Resource id for the string * @return The string data associated with the resource, stripped of styled * text information. */ @NonNull public final String getString(@StringRes int resId) { return getResources().getString(resId); } /** * Returns a localized formatted string from the application's package's * default string table, substituting the format arguments as defined in * {@link java.util.Formatter} and {@link java.lang.String#format}. * * @param resId Resource id for the format string * @param formatArgs The format arguments that will be used for * substitution. * @return The string data associated with the resource, formatted and * stripped of styled text information. */ @NonNull public final String getString(@StringRes int resId, Object... formatArgs) { return getResources().getString(resId, formatArgs); }
两个方法,一个是直接获取字符串,一个是带参数的字符串。
哎,还有好多好学西的,加油!
相关文章推荐
- Android动画
- android(NDK+JNI)---jni开发资料
- android开发框架(五)AIDL进程间通信机制
- Android样式的开发:shape篇
- Android proguard 详解
- Android Studio多渠道打包
- Android应用中使用XmlSerializer序列化XML数据的教程
- android(NDK+JNI)---NDK编译生成so文件
- Android进程调度cgroups的简单介绍
- Android:控件Spinner实现下拉列表
- Android 中的 Service 全面总结
- Android应用市场(发布APK)及多渠道打包
- android(NDK+JNI)---NDK入门学习
- 《Android源码设计模式》读书笔记 (22) 第22章 外观模式
- 搭建android开发环境
- android:exported
- Android——SQLite数据库(一)创建数据库、创建表、初始化数据
- android6.0 adbd深入分析(一)adb驱动初始化、读取adb节点线程
- Android 源码集合
- Android-ClockView