【Android】操作资源文件

main.xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <TextView
        android:id="@+id/mytext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
 
</LinearLayout>

.java代码如下:

package org.lxh.demo;
 
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
 
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
 
public class Hello extends Activity {
	private TextView mytext = null;
 
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState); // 生命周期方法
		super.setContentView(R.layout.main); // 设置要使用的布局管理器
		this.mytext = (TextView) super.findViewById(R.id.mytext);
		Resources res = super.getResources();// 资源操作类
		InputStream input = res.openRawResource(R.raw.mybook);//读取资源id
		Scanner scan = new Scanner(input);//
		StringBuffer buf = new StringBuffer();//用于接收数据
		while (scan.hasNext()) {
			buf.append(scan.next()).append("\n");
		}
		scan.close();
		try {
			input.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
 
		this.mytext.setText(buf.toString());
	}
}

在这里插入图片描述

发布了73 篇原创文章 · 获赞 17 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_40110781/article/details/104940641