Android学习笔记(四) —— 置换TextView文字 — CharSequence数据类型与Resource ID应用
2012-11-09 15:07
429 查看
从一开始自Layout里通过Resource初始化TextView的文字,到程序中动态更改TextView文字,但要如何在代码里取得Resource的字符串呢?在Android里,确实是有些方法可以直接以R.string.*直接转换ID为String,不过,这样的数据类型转换是非常规甚至是不妥的,正确的方法是利用Context.getString方法来取得存放在global里的Resource ID。以下这个范例将示范如何在程序运行时(runtime),通过CharSequence依据Resource
ID取出字符串,并正确更改TextView的文字。
运行结果:
![](http://img.my.csdn.net/uploads/201211/09/1352445128_4386.jpg)
步骤:
step1:新建Android项目EX03_04_CharSequenceDemo
step2:res->values->strings.xml
<resources>
<string name="app_name">EX03_04_CharSequenceDemo</string>
<string name="hello_world">Hello world,EX03_04_CharSequenceDemo!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_ex03_04__char_sequence_demo">EX03_04_CharSequenceDemo</string>
<string name="str_1">我是Resource里的\"字符串1\"</string>
<string name="str_2">\"字符串2\"</string>
</resources>
step3:res->valuse添加color.xml
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<color name="white">#FFFFFF</color>
<color name="black">#000000</color>
<color name="red">#FF0000</color>
<color name="darkgray">#A9A9A9</color>
<color name="lightskyblue">#87CEFA</color>
<color name="pink">#FFC0CB</color>
<color name="lightpink">#FFB6C1</color>
<color name="darkblue">#00008B</color>
</resources>
step4:res->layout->main.xml
作为对比,在main.xml里创建了两个TextView对象,并采用LinearLayout的方式配置,一上一下,在运行结果中id为myTextView01的TextView并没有任何文字的更改,维持一开始的str_1,但在程序运行后,id为myTextView02的TextView则做了文字的实时更改。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="@color/pink"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/myTextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_1"
android:layout_x="30px"
android:layout_y="50px"/>
<TextView
android:id="@+id/myTextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_2"
android:layout_x="30px"
android:layout_y="70px"/>
</LinearLayout>
step5:EX03_04_CharSequenceDemo
在更改mTextView02的文字时(setText方法),合并了str_3与str_2这两个不同对象,由于setText方法同事支持CharSequence与String类型的参数,故在此示范不同数据类型的字符串进行同步输出。
package com.example.ex03_04_charsequencedemo;
import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;
public class EX03_04_CharSequenceDemo extends Activity {
private TextView mTextView02;
/**Called when the activity is first created*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView02 = (TextView)findViewById(R.id.myTextView02);
CharSequence str_2 = getString(R.string.str_2);
String str_3 = "我是程序中调用Resource的";
mTextView02.setText(str_3 + str_2);
}
}
ID取出字符串,并正确更改TextView的文字。
运行结果:
![](http://img.my.csdn.net/uploads/201211/09/1352445128_4386.jpg)
步骤:
step1:新建Android项目EX03_04_CharSequenceDemo
step2:res->values->strings.xml
<resources>
<string name="app_name">EX03_04_CharSequenceDemo</string>
<string name="hello_world">Hello world,EX03_04_CharSequenceDemo!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_ex03_04__char_sequence_demo">EX03_04_CharSequenceDemo</string>
<string name="str_1">我是Resource里的\"字符串1\"</string>
<string name="str_2">\"字符串2\"</string>
</resources>
step3:res->valuse添加color.xml
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<color name="white">#FFFFFF</color>
<color name="black">#000000</color>
<color name="red">#FF0000</color>
<color name="darkgray">#A9A9A9</color>
<color name="lightskyblue">#87CEFA</color>
<color name="pink">#FFC0CB</color>
<color name="lightpink">#FFB6C1</color>
<color name="darkblue">#00008B</color>
</resources>
step4:res->layout->main.xml
作为对比,在main.xml里创建了两个TextView对象,并采用LinearLayout的方式配置,一上一下,在运行结果中id为myTextView01的TextView并没有任何文字的更改,维持一开始的str_1,但在程序运行后,id为myTextView02的TextView则做了文字的实时更改。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="@color/pink"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/myTextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_1"
android:layout_x="30px"
android:layout_y="50px"/>
<TextView
android:id="@+id/myTextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_2"
android:layout_x="30px"
android:layout_y="70px"/>
</LinearLayout>
step5:EX03_04_CharSequenceDemo
在更改mTextView02的文字时(setText方法),合并了str_3与str_2这两个不同对象,由于setText方法同事支持CharSequence与String类型的参数,故在此示范不同数据类型的字符串进行同步输出。
package com.example.ex03_04_charsequencedemo;
import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;
public class EX03_04_CharSequenceDemo extends Activity {
private TextView mTextView02;
/**Called when the activity is first created*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView02 = (TextView)findViewById(R.id.myTextView02);
CharSequence str_2 = getString(R.string.str_2);
String str_3 = "我是程序中调用Resource的";
mTextView02.setText(str_3 + str_2);
}
}
相关文章推荐
- 置换TextView文字----CharSequence数据类型与Resource ID应用
- CharSequence数据类型与Resource ID
- ArrayAdapter requires the resource ID to be a TextView问题
- 长varchar值与text数据类型在应用中的一些问题
- 将文字TextView的内容斜过来,应用例如:商品促销的标签
- Android SDK开发范例大全 ---3.4置换TextView文字
- ArrayAdapter requires the resource ID to be a TextView问题
- You must supply a resourceID for a TextView
- 如何解决ArrayAdapter requires the resource ID to be a TextView
- ArrayAdapter requires the resource ID to be a TextView 的解决办法
- You must supply a resourceID for a TextView
- Android学习笔记(一) —— 更改与显示文字标签—TextView标签的使用
- ArrayAdapter requires the resource ID to be a TextView问题
- ArrayAdapter requires the resource ID to be a TextView问题整理
- ArrayAdapter的用法:ArrayAdapter(Context context, int textViewResourceId, T[] objects)
- ArrayAdapter requires the resource ID to be a TextView问题
- 如何解决ArrayAdapter requires the resource ID to be a TextView
- 数据类型TextView中添入int型的错误
- Android学习笔记(三) —— 更改TextView文字颜色及文字背景色
- ArrayAdapter requires the resource ID to be a TextView错误