res/layout/main.xml
<?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:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/tvText" android:text="@string/tv_name"/> <Button android:layout_width="wrap_content" android:id="@+id/btnChangeColor" android:layout_height="wrap_content" android:text="@string/btn_name"></Button> </LinearLayout>
src/ex03_12.java
package gphone.ex03_12; 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 EX03_12 extends Activity { Button btnChangeColor=null; TextView tvText=null; //用于存储颜色 int[] colors=null; int color_index; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //存储颜色 colors=new int[]{ Color.RED, Color.BLUE, Color.YELLOW, Color.GREEN, Color.GRAY }; color_index=0; tvText=(TextView)EX03_12.this.findViewById(R.id.tvText); btnChangeColor=(Button)EX03_12.this.findViewById(R.id.btnChangeColor); btnChangeColor.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { // 按顺序显示定义的颜色 if(color_index<colors.length) { tvText.setTextColor(colors[color_index]); } else { color_index=0;
color_index++;
} } }); } }
运行结果