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()); } }
运行如下:
时间: 2024-10-03 20:53:17