我正在开发一个具有完整UI自定义功能的应用。如果您查看@Controller
public class ResourceHttpRequestHandlerReplacer extends ResourceHttpRequestHandler implements BeanFactoryPostProcessor {
@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
super.handleRequest(request, response);
}
public void postProcessBeanFactory(ConfigurableListableBeanFactory factory)
throws BeansException {
String[] names = factory.getBeanNamesForType(ResourceHttpRequestHandler.class);
for (String name : names) {
BeanDefinition bd = factory.getBeanDefinition(name);
bd.setBeanClassName("com.project.controllers.ResourceHttpRequestHandlerReplacer");
}
}
}
中包含的EditText
,则TextInputLayout
的默认文字提示颜色和线条颜色与样式中定义的颜色EditText
相同。但是,当textPrimaryColor
获得焦点时,该线条将成为样式中定义的EditText
的颜色,提示将通过浮动提示动画并更改为accent
颜色。使用accent
和styles.xml
非常容易,但是,我无法以编程方式完成它。
我设置的当前方法是:
themes.xml
这种方法的问题在于:A)提示文字立即着色为强调色,当收到焦点时,它没有着色为重音B)public static void setInputTextLayoutColor(final int accent, final int text, TextInputLayout textInputLayout, AppCompatEditText edit) {
edit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
AppCompatEditText editText = (AppCompatEditText) v;
editText.getBackground().clearColorFilter();
if(hasFocus) editText.getBackground().setColorFilter(accent, PorterDuff.Mode.SRC_IN);
else editText.getBackground().setColorFilter(text, PorterDuff.Mode.SRC_IN);
}
});
setCursorColor(edit, accent);
try {
Field field = textInputLayout.getClass().getDeclaredField("mFocusedTextColor");
field.setAccessible(true);
int[][] states = new int[][]{
new int[]{}
};
int[] colors = new int[]{
accent
};
ColorStateList myList = new ColorStateList(states, colors);
field.set(textInputLayout, myList);
Field fDefaultTextColor = TextInputLayout.class.getDeclaredField("mDefaultTextColor");
fDefaultTextColor.setAccessible(true);
fDefaultTextColor.set(textInputLayout, myList);
Method method = textInputLayout.getClass().getDeclaredMethod("updateLabelState", boolean.class);
method.setAccessible(true);
method.invoke(textInputLayout, true);
} catch (Exception e) {
e.printStackTrace();
}
}
行以正确的颜色开始,但是当它获得焦点时,它会着色为样式中定义的EditText
,而不是编程设置的颜色。
答案 0 :(得分:0)
我无法弄清楚动态改变提示颜色,但我得到的线条颜色随着听众改变:
AppCompatEditText edit = (AppCompatEditText)findViewById(R.id.event_create_name_edit);
edit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
AppCompatEditText edit2 = (AppCompatEditText)v;
if(hasFocus) edit2.setSupportBackgroundTintList(ColorStateList.valueOf(rui.getAccent()));
else edit2.setSupportBackgroundTintList(ColorStateList.valueOf(rui.getText()));
}
});