首先打开”main.xml”布局文件
,添加TextView命名为tvHelloWorld:保存。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/tvHelloWorld" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout>
这时候Eclipse编译器会自动生成TextView代码,打开R.java文件
可以看到
/* AUTO-GENERATED FILE. DO NOT MODIFY. * * This class was automatically generated by the * aapt tool from the resource data it found. It * should not be modified by hand. */ package idroidgame.ActivityTest; public final class R { public static final class attr { } public static final class drawable { public static final int icon=0x7f020000; } public static final class id { public static final int tvHelloWorld=0x7f050000; } public static final class layout { public static final int main=0x7f030000; } public static final class string { public static final int app_name=0x7f040001; public static final int hello=0x7f040000; } }
然后在ActivityText.java中输入
package idroidgame.ActivityTest; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class ActivityTest extends Activity { TextView tv=null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv=(TextView)this.findViewById(R.id.tvHelloWorld); String str="设置TextView的值"; tv.setText(str); } }
TextView的setText有很多重载方法;