1、新建一个xml文件:button_text
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="#ff2525"></item>
<item android:color="#ff5b5b5b"></item>
</selector>
2、创建ColorStateList对象并使用
ColorStateList csl=(ColorStateList)getResources().getColorStateList(R.color.button_text);
for (int i =0;i<3;i++){
Button btn = new Button(mContext);
btn.setText("按钮"+i);
btn.setTextColor(csl);
this.addView(btn,params);
}
这样做的好处就是不用每次都在hasfocus函数中加setTextColor