//ActivityTextview.java
package android.ActivityUI;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class ActivityTextview extends Activity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.textview);
setTitle("TextView");
//get TextView object
TextView textview = (TextView)findViewById(R.id.textview);
String string = "Welcome use TextView";
//set text color
textview.setTextColor(Color.RED);
//set text font size
textview.setTextSize(20);
//set background color
textview.setBackgroundColor(Color.BLUE);
//set TextView's display
textview.setText(string);
Button button_change = (Button)findViewById(R.id.button_change);
button_change.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
TextView text_view = (TextView)findViewById(R.id.textview);
CharSequence text_view_old = text_view.getText();
text_view.setText(text_view_old+"add:I am happy");
}
});
}
}
//textview.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:text="TextView"
android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/button_change"
android:text="Change">
</Button>
</LinearLayout>
点击Change按钮后: