Android数据访问(二)——Resource
2015-03-30 22:52
218 查看
### 使用Resource访问程序数据##
———-
Resource一般用来取得res目录内的资源。要取得res目录内的资源,基本都是通过资源的resource ID来取得,即通过R文件。这种方法,可以取得很多类型的文件,如文本文件,图像,影音,还有UI组件。我们刚开始写Android程序时,用findViewById()方法来获取控件,就是这种方法的典型应用。下面的例子中,通过这种方法来获取文本,图片和音频。
这里只写主要代码,其他由编译器自动生成的代码就不写了。
activity_main.xml文件
在strings.xml中添加一行代码
“` 永别了武器 海明威 “`
这个字符串是带格式的,“`永别了武器 “`表示的是文字带有下划线。
然后是MainActivity.java文件:
运行结果
———-
Resource一般用来取得res目录内的资源。要取得res目录内的资源,基本都是通过资源的resource ID来取得,即通过R文件。这种方法,可以取得很多类型的文件,如文本文件,图像,影音,还有UI组件。我们刚开始写Android程序时,用findViewById()方法来获取控件,就是这种方法的典型应用。下面的例子中,通过这种方法来获取文本,图片和音频。
这里只写主要代码,其他由编译器自动生成的代码就不写了。
activity_main.xml文件
//这里写了3个TextView和一个ImageView,用来显示取得的文字和图像 <TextView android:id="@+id/strText" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/hello_world" /> <TextView android:id="@+id/txtText" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/strText" android:layout_below="@+id/strText" android:text="@string/hello_world" /> <TextView android:id="@+id/recText" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/txtText" android:layout_below="@+id/txtText" android:text="@string/hello_world" /> <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/recText" android:layout_alignLeft="@+id/strText" />
在strings.xml中添加一行代码
“` 永别了武器 海明威 “`
这个字符串是带格式的,“`永别了武器 “`表示的是文字带有下划线。
然后是MainActivity.java文件:
public class MainActivity extends Activity { TextView strText = null; TextView txtText = null; TextView recText = null; ImageView img = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViews(); } public void findViews(){ strText = (TextView)findViewById(R.id.strText); txtText = (TextView)findViewById(R.id.txtText); recText = (TextView)findViewById(R.id.recText); img = (ImageView)findViewById(R.id.img); Resources res = this.getResources();//获取Resource对象 //注意下面三种获取字符串的方法 CharSequence text1 = getString(R.string.info); //获取的文本不保留格式,只保留纯文字 CharSequence text2 = getText(R.string.info); //获取的文本保留了格式 CharSequence text3 = getText(R.string.info); //仍然获取有格式的字符串,这里要特别注意一下,待会儿看下面的代码 strText.setText(text1);//第一个TextView中设置没有格式的字符串 txtText.setText(text2);//第二个TextView中设置有格式的字符串 recText.setText("Resources:"+text3); //第三个textView中,将有格式的字符串text3和一个普通字符串"Resources:"相加一下,可以看到下面的程序运行结果,发现"永别了,武器"这一行字的下划线没有了,这说明一个有格式的字符串和一个普通无符号字符串相加,会进行强制类型转换,结果是一个无符号的字符串 img.setImageDrawable(res.getDrawable(R.drawable.beautiful_love_wallpapers_for_twitter));//获取drawable文件夹下的一张图 MediaPlayer mp = MediaPlayer.create(MainActivity.this,R.raw.when_we_were_young);//获取raw文件夹下的一个音频文件,程序运行时会自动播放歌曲 mp.start();//开始播放 } }
运行结果
相关文章推荐
- android数据存储与访问
- Android 应用程序之间数据共享—ContentProvider 保时被访问
- 怎么通过web服务器访问MYSQL数据库,使其数据同步到android SQLite数据库?
- Android深入探究笔记之十 -- 使用 ContentProvider 共享数据(二),访问与添加通讯录
- android通过 httpClient 的post访问 网络数据
- Android(二)数据存储和访问 之文件
- 5.4、Android中数据存储和访问-使用ContentProvider共享数据
- Android访问中央气象台的天气预报API得到天气数据
- R.raw等R下自建文件夹下文件的访问(Android指向Resource的URI)
- Android 数据存储与访问 SharedPreferences
- [android]webview访问jsp页面,能否从jsp页面获取数据
- (android 互联网开发)2 访问https接口,进行数据交互(已在实际项目中使用)
- Android 内容提供器---创建内容提供器(Intent对象和数据访问)
- android 数据存储和访问方式二:SharedPreferences(共享参数)详解
- Android 学习笔记7---数据存储与访问
- Android深入探究笔记之十 -- 使用 ContentProvider 共享数据(二),访问与添加通讯录
- android 数据存储和访问方式一:文件详解
- android 数据的存储和访问以及单元测试的引入
- Android访问中央气象台的天气预报API得到天气数据
- Android数据存储与访问(10级学员张晓丛)