1.Java文件
/**
*
*/
package com.itest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
/**
* @author Robin
*/
public class ITestP0Activity extends Activity {
Button b1;
TextView t1;
/** Called when the activity is first created. */
@Override
public void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
//载入布局文件中的布局
setContentView( R.layout.p0 );
//查找控件
b1 = (Button) findViewById( R.id.button1 );
t1 = (TextView) findViewById( R.id.textView1 );
//增加监听事件
b1.setOnClickListener( new OnClickListener() {
@Override
public void onClick( View v )
{
t1.setText( System.currentTimeMillis()+" Hello World" );
}
} );
}
}
2.p0.xml布局文件(线性布局,含一个TextView,一个Button)
<?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/textView1" android:text="TextView"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/changeText"></Button>
</LinearLayout>
3.点按钮显示Hello World!