Android TextInputLayout以编程方式设置样式

时间:2017-06-03 14:53:51

标签: android

我正在开发一个具有完整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颜色。使用accentstyles.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,而不是编程设置的颜色。

这是非聚焦图片(参考“事件名称”字段)。这里,“事件名称”的颜色应为灰色/白色(如文本“): Non-focused <code>TextInputLayout</code>

这是专注的图片,在这里,除了线条的颜色外,一切都是正确的,也应该是绿色。 Focused <code>TextInputLayout</code>

1 个答案:

答案 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()));
        }
    });