int[][] states = new int[][]{
new int[]{-android.R.attr.state_focused}, // enabled
new int[]{android.R.attr.state_focused}, // disabled
};
int[] colors = new int[]{
secondaryColor,
primaryColor,
};
然后创建ColorStateList变量
ColorStateList myList = new ColorStateList(states, colors);
最后一步是将其分配给edittext。
editText.setBackgroundTintList(myList);
在这之后,你必须写焦点变化事件。
this.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
setUnderlineColor(selectionColor,deselectionColor);
}
});
你可以在setUnderlineClor()方法中创建上述代码,
private void setUnderlineColor(int primaryColor, int secondaryColor) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
int[][] states = new int[][]{
new int[]{-android.R.attr.state_focused}, // enabled
new int[]{android.R.attr.state_focused}, // disabled
};
int[] colors = new int[]{
secondaryColor,
primaryColor,
};
ColorStateList myList = new ColorStateList(states, colors);
setBackgroundTintList(myList);
}
}