標籤: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);//讀取資源idScanner 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 blocke.printStackTrace();}this.mytext.setText(buf.toString());}}
運行如下:
安卓--操作資源檔