效果:
[img]http://dl.iteye.com/upload/attachment/380702/6c686565-79e4-353d-a8bf-08be46f8a3a1.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/380704/5a33ae64-3db4-3d2f-bcea-71220399f0d4.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/380706/3cace52f-3d67-3984-ad2b-b821c649d02e.jpg[/img]
main.xml
color.xml
[img]http://dl.iteye.com/upload/attachment/380702/6c686565-79e4-353d-a8bf-08be46f8a3a1.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/380704/5a33ae64-3db4-3d2f-bcea-71220399f0d4.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/380706/3cace52f-3d67-3984-ad2b-b821c649d02e.jpg[/img]
main.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<Button
android:id="@+id/setColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="换颜色"
android:layout_x="116px"
android:layout_y="202px"
>
</Button>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="21px"
android:text="你好啊!!!"
android:layout_x="100px"
android:layout_y="143px"
>
</TextView>
</AbsoluteLayout>
color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="red">#FF0000</color>
<color name="green">#00FF00</color>
<color name="yellow">#FFFF00</color>
<color name="bule">#0000FF</color>
</resources>
package set.textView.color;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class SetTextViewColor extends Activity {
private int[] arry;
private int count=0;
private TextView text;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**载入main.xml*/
setContentView(R.layout.main);
/**通过main.xml找到Button组件*/
Button bt=(Button)findViewById(R.id.setColor);
/**通过main.xml找到TextView组件*/
text=(TextView)findViewById(R.id.text);
/**设置一个数组存入颜色*/
arry=new int[]{R.color.red,R.color.bule,R.color.green,R.color.yellow};
/**设置按钮点击事件*/
bt.setOnClickListener(new Button.OnClickListener(){
/**改变TextView的颜色*/
public void onClick(View v){
int myColor=0;
if(count<arry.length){
/**获取color.xml文件里的配置项*/
myColor=getResources().getColor(arry[count]);
/**设置TextView内容*/
text.setTextColor(myColor);
count++;
}else{
count=0;
}
}
});
}
}