代码设置文本点击按下颜色变化
[html] view plaincopy
1. <?xml version="1.0" encoding="utf-8"?>
2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
3. <item android:state_pressed="true"
4. android:color="#ffff0000"/> <!-- pressed -->
5. <item android:state_focused="true"
6. android:color="#ff0000ff"/> <!-- focused -->
7. <item android:color="#ff000000"/> <!-- default -->
8. </selector>
上面代码是一个选择器,通常情况我们都在布局文件中添加这样的代码android:background = "@drawable/选择器的文件名" 来实现背景或颜色切换的,可要是用对应功能的代码实现是什么样子呢?
[java] view plaincopy
1. <strong>ColorStateList.java 这个类 </strong>
2. Button btn=(Button)findViewById(R.id.btn);
3. Resources resource=(Resources)getBaseContext().getResources();
4. ColorStateList csl=(ColorStateList)resource.getColorStateList(R.color.button_text);
5. if(csl!=null){
6. //设置按钮文字颜色
7. }
ColorStateList.java 这个类的