Android-字符串资源及其格式化
来源:互联网 发布:100以内的素数c语言 编辑:程序博客网 时间:2024/05/16 16:03
1、纯文本字符串
字符串的表示分以下三种情况:
a) 普通字符串(不含双引号(”)及单引号(’))。其在XML文件中如代码一所示定义。
代码一:
- <?xml
version= "1.0"encoding= "utf-8"?> - <resources>
-
<string name= World!</string> - </resources>
b)
代码二:
- <?xml
version= "1.0"encoding= "utf-8"?> - <resources>
- <string
name= "hello">"Hello'World!" </string> - </resources>
代码三:
- <?xml
version= "1.0"encoding= "utf-8"?> - <resources>
-
<string name= World!</string> - </resources>
c)
代码四:
- <?xml
version= "1.0"encoding= "utf-8"?> - <resources>
-
<string name= World/"!</string> - </resources>
2、格式字符串
代码五:
- <?xml
version= "1.0"encoding= "utf-8"?> - <resources>
-
<string name= World, my name is %1$s!</string> -
<string name= - </resources>
代码六:
- TextView
mytext = (TextView)findViewById(R.id.mystring); - String
myname = getString(R.string.hello); - myname
= "clark");String.format(myname, - mytext.setText(myname);
图1:
3、样式字符串
a) 直接将HTML标记写入字符串资源中,同时在布局文件中直接引用。这里给出一个例子,代码七为文件strings.xml中的内容,代码八为布局文件的内 容,代码九则给出了Activity中onCreate函数中的内容,图2则给出了最终的效果图。这种情况下,能使用的HTML标记仅 为<i>、<b>及<u>三种。
代码七:
- <?xml
version= "1.0"encoding= "utf-8"?> - <resources>
-
<string name= World,</b> <i>my name is</i> <u>clark</u>!</string> -
<string name= - </resources>
代码八:
- <?xml
version= "1.0"encoding= "utf-8"?> - <LinearLayout
xmlns:android= "http://schemas.android.com/apk/res/android" -
android:orientation= -
android:layout_width= -
android:layout_height= -
> - <TextView
-
android:id= -
android:layout_width= -
android:layout_height= -
android:text= -
/> - </LinearLayout>
代码九:
- public
void onCreate(Bundle savedInstanceState) { -
super.onCreate(savedInstanceState); -
setContentView(R.layout.main); - }
图2:
b)
代码十:
- <?xml
version= "1.0"encoding= "utf-8"?> - <resources>
-
<string name= World,</b> <i>my name is</i> <u>clark</u>!</string> -
<string name= - </resources>
代码十一:
- <?xml
version= "1.0"encoding= "utf-8"?> - <LinearLayout
xmlns:android= "http://schemas.android.com/apk/res/android" -
android:orientation= -
android:layout_width= -
android:layout_height= -
> - <TextView
-
android:id= -
android:layout_width= -
android:layout_height= -
android:text= -
/> - </LinearLayout>
代码十二:
- public
void onCreate(Bundle savedInstanceState) { -
super.onCreate(savedInstanceState); -
setContentView(R.layout.main); -
TextView mytext = (TextView)findViewById(R.id.mystring); -
-
String myname = getString(R.string.hello); -
Spanned textspan = Html.fromHtml(myname); -
mytext.setText(textspan); - }
c)
代码十三:
- public
void onCreate(Bundle savedInstanceState) { -
super.onCreate(savedInstanceState); -
setContentView(R.layout.main); -
TextView mytext = (TextView)findViewById(R.id.mystring); -
mytext.setText(getResources().getText(R.string.hello)); - }
d)
代码十四:
- <?xml
version= "1.0"encoding= "utf-8"?> - <resources>
-
<string name= World, my name is clark!</string> -
<string name= - </resources>
代码十五:
- <?xml
version= "1.0"encoding= "utf-8"?> - <LinearLayout
xmlns:android= "http://schemas.android.com/apk/res/android" -
android:orientation= -
android:layout_width= -
android:layout_height= -
> - <EditText
-
android:id= -
android:layout_width= -
android:layout_height= -
android:text= -
/> - </LinearLayout>
代码十六:
- public
void onCreate(Bundle savedInstanceState) { -
super.onCreate(savedInstanceState); -
setContentView(R.layout.main); -
EditText mytext = (EditText)findViewById(R.id.mystring); -
mytext.setText(R.string.hello); -
Spannable spn = mytext.getText(); -
spn.setSpan( BackgroundColorSpan(Color.GRAY), 0, 11, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); -
mytext.setText(spn); - }
图3:
4、样式字符串格式化
对样式字符串进行格式化,有以下几个步骤:
a)
b) 按一般情况检索字符串资源。例如,getString(R.string.hello)。
c) 生成格式结果,确保转义你替换的任何字符串值,防止它们包含尖括号或&符号。例如,String.format(getString(R.string.hello),TextUtils.htmlEncode(name))。
d) 通过Html.fromHtml()将实体转义的HTML转换为Spanned对象。例如,Spanned textspan = Html.fromHtml(myname)。
这里给出一个例子,代码十七为文件strings.xml中的内容,代码十一为布局文件的内容,代码十八则给出了Activity中onCreate函数中的内容,其效果如图2所示。
代码十七:
- <?xml
version= "1.0"encoding= "utf-8"?> - <resources>
-
<string name= World,</b> <i>my name is</i> <u>%1$s</u>!</string> -
<string name= - </resources>
代码十八:
- public
void onCreate(Bundle savedInstanceState) { -
super.onCreate(savedInstanceState); -
setContentView(R.layout.main); -
TextView mytext = (TextView)findViewById(R.id.mystring); -
String myname = getString(R.string.hello); -
myname = String.format(myname, -
Spanned textspan = Html.fromHtml(myname); -
mytext.setText(textspan); - }
- Android字符串资源及其格式化
- Android字符串资源及其格式化
- Android字符串资源及其格式化
- Android-字符串资源及其格式化
- Android字符串资源及其格式化
- Android字符串资源及其格式化
- Android字符串资源及其格式化
- Android字符串资源及其格式化
- Android字符串资源及其格式化
- Android字符串资源及其格式化
- android代码重构之字符串资源处理及其格式化
- Android 格式化字符串资源
- Android:string资源格式化字符串
- [android] 资源中的格式化字符串
- android代码重构日记(三)——字符串资源处理及其格式化
- 代码重构-字符串资源处理及其格式化
- android字符串资源以及格式化记录
- 字符串:格式化及其方法
- spidermonkey 分析
- 教你如何使用飞信免费定时给自己发短信
- 程序员创业之路——华章图书永远相伴
- 以小见大、由浅入深-谈如何面试Javascript工程师
- Null value was assigned to a property of primitive type setter of com.index.
- Android-字符串资源及其格式化
- SQL 模糊查询
- C# 结构体和字节数组的转换
- css中link和import的区别
- Visual Studio 2010 实用功能总结
- 如何防止代码腐烂
- 关于SAP程序版本问题
- Android 4.0 Ice Cream Sandwich源码发布,source code release啦
- log4j.properties配置详解