CharSequence的getText()与String的getString()(转)
CharSequence的getText()与String的getString()『Android系列七』
曾经在学习中碰见两种获取常量的方式:
CharSequence chrs = getText(R.string.demo);
String str = getString(R.string.demo);
这两种方式有什么不同呢?一定要搞明白,开始实验:
实验一:strings.xml中的相关代码<string name="demo">demo</string>
main.xml中用线性布局定义两个文本标签,分别使用两种方式获取demo的值,详细代码
- <LinearLayout 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:id="@+id/firstText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="" />
- <TextView
- android:id="@+id/secondText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="" />
- </LinearLayout>
<LinearLayout 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:id="@+id/firstText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" /> <TextView
android:id="@+id/secondText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" /> </LinearLayout>
注意android:text的值都是空的,下面继续看main.java是怎么处理的:
- package com.dy.study.firstbase;
- import android.os.Bundle;
- import android.widget.TextView;
- import android.app.Activity;
- public class Main extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- findViews();
- change();
- }
- private TextView firstText;
- private TextView secondText;
- private void findViews() {
- firstText = (TextView) findViewById(R.id.firstText);
- secondText = (TextView) findViewById(R.id.secondText);
- }
- private void change() {
- CharSequence chs = getText(R.string.demo);
- String str = getString(R.string.demo);
- firstText.setText(chs);
- secondText.setText(str);
- }
- }
package com.dy.study.firstbase; import android.os.Bundle;
import android.widget.TextView;
import android.app.Activity; public class Main extends Activity { @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); findViews(); change();
} private TextView firstText;
private TextView secondText; private void findViews() {
firstText = (TextView) findViewById(R.id.firstText);
secondText = (TextView) findViewById(R.id.secondText);
} private void change() {
CharSequence chs = getText(R.string.demo);
String str = getString(R.string.demo);
firstText.setText(chs);
secondText.setText(str);
}
}
好,看看实验一的结果:
可以看到,两个值完全一样,怎么回事?难道这两种方式的效果时完全一样的吗?先不忙下结论,继续实验。
实验二:把strings.xml中的那句相关代码改成<string name="demo"><b>demo</b></string>,仔细看,中间加了个<b></b>,然后其他都不改变,看结果:
你看,前面那个标签变了,这说明CharSequence的getText()是获取格式化的常量值,而String的getString()是单单获取值,而舍去了格式化。
就这么简单吗?可我又想了,如果把它们加起来,会是什么样子呢,继续实验。
实验三:在实验二的基础上改下main.java的内容:
main.java
- package com.dy.study.firstbase;
- import android.os.Bundle;
- import android.widget.TextView;
- import android.app.Activity;
- public class Main extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- findViews();
- change();
- }
- private TextView firstText;
- private void findViews() {
- firstText = (TextView) findViewById(R.id.firstText);
- }
- private void change() {
- CharSequence chs = getText(R.string.demo);
- String str = getString(R.string.demo);
- firstText.setText(chs + str);
- }
- }
package com.dy.study.firstbase; import android.os.Bundle;
import android.widget.TextView;
import android.app.Activity; public class Main extends Activity { @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); findViews(); change();
} private TextView firstText; private void findViews() {
firstText = (TextView) findViewById(R.id.firstText);
} private void change() {
CharSequence chs = getText(R.string.demo);
String str = getString(R.string.demo);
firstText.setText(chs + str);
}
}
这里只让显示标签一的内容,猜想一下是不是一个粗一个细呢?看结果:
被同化了,这让我想起了java的隐式转换,实验到此为止。
CharSequence的getText()与String的getString()(转)的更多相关文章
- 0703-APP-Notification-statue-bar
1.展示显示textTicker和仅仅有icon的两种情况:当參数showTicker为true时显示否则不显示 // In this sample, we'll use the same text ...
- 一个帖子学会Android开发四大组件
来自:http://www.cnblogs.com/pepcod/archive/2013/02/11/2937403.html 这个文章主要是讲Android开发的四大组件,本文主要分为 一.Act ...
- Android Service学习之本地服务
Service是在一段不定的时间运行在后台,不和用户交互应用组件.每个Service必须在manifest中 通过来声明.可以通过contect.startservice和contect.bindse ...
- 一天就学会Android开发四大组件
这个文章主要是讲Android开发的四大组件,本文主要分为 一.Activity详解二.Service详解三.Broadcast Receiver详解四.Content Provider详解外加一个重 ...
- android显示通知栏Notification以及自定义Notification的View
遇到的最大的问题是监听不到用户清除通知栏的广播.所以是不能监听到的. 自定义通知栏的View,然后service运行时更改notification的信息. /** * Show a notificat ...
- android 四大组件详解
这个文章主要是讲Android开发的四大组件,本文主要分为 一.Activity详解二.Service详解三.Broadcast Receiver详解四.Content Provider详解外加一个重 ...
- 9-第一个app项目
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- 关于Android开发四大组件
文章主要是讲Android开发的四大组件,本文主要分为 文章源自:http://www.cnblogs.com/pepcod/archive/2013/02/11/2937403.html 一.Act ...
- String详解, String和CharSequence区别, StringBuilder和StringBuffer的区别 (String系列之1)
本章主要介绍String和CharSequence的区别,以及它们的API详细使用方法. 转载请注明出处:http://www.cnblogs.com/skywang12345/p/string01. ...
随机推荐
- Chapter 2 Open Book——8
But as far as I could tell, life worked that way most of the time. 但是即使我这么说,生活大多数时间还是这样的. 但就我所能告诉你的, ...
- .NET WinForm程序中给DataGridView表头添加下拉列表实现数据过滤
转:http://www.cnblogs.com/jaxu/archive/2011/08/04/2127365.html 我们见过Excel中的数据过滤功能,可以通过点击表头上的下拉列表来实现数据的 ...
- iOS打包app发给测试人员测试
说明:在项目开发过程中经常需要开发人员将项目打包成ipa包后,发给测试人员进行测试.本文贴图对打包的过程简单介绍. 一.Product ->archive (注意,不能是模拟器状态,如果当前调试 ...
- iOS 的 APP 如何适应 iPhone 5s/6/6Plus 三种屏幕的尺寸?
初代iPhone 2007年,初代iPhone发布,屏幕的宽高是 320 x 480 像素.下文也是按照宽度,高度的顺序排列.这个分辨率一直到iPhone 3GS也保持不变. 那时编写iOS的App( ...
- 百度前端面试题-类似slack的在线聊天室
别人国庆出去玩,我在家写代码的感觉也是很不错哒. 首先介绍一下技术架构吧! 使用了js框架:FFF,zepto,jquery,md5.min.js 前端框架:Bootstrap 后端:野狗,部分PHP ...
- Ant 参考
http://ant.apache.org/manual/Tasks/exec.html Exec Description Executes a system command. When the os ...
- Android OpenGL ES(十三)通用的矩阵变换指令 .
Android OpenGL ES 对于不同坐标系下坐标变换,大都使用矩阵运算的方法来定义和实现的.这里介绍对应指定的坐标系(比如viewmodel, projection或是viewport) An ...
- Hibernate 系列教程4-单向多对一
项目图片 hibernate.cfg.xml <mapping resource="com/jege/hibernate/one/way/manytoone/User.hbm.xml& ...
- iOS UIScrollView偏移量属性
contentSize: The size of the content view. 其实就是scrollview可以滚动的区域,比如frame = (0 ,0 ,320 ,480) contentS ...
- more分页阅读
相比cat命令,more可以更加灵活的去阅读查看文件. 1.命令格式 more [-dlfpcsu ] [-num ] [+/ pattern] [+ linenum] [file ... ] 2.命 ...