带有左图标的Android自定义EditText可以在edittext不为空时更改Focus的颜色并保持图标颜色在焦点上吗?

时间:2018-12-18 01:06:23

标签: android android-studio android-layout android-edittext

我正在寻找什么?

  

当editTextView不为空时,我想使图标可绘制在焦点上

我有一个自定义的 editTextView ,它具有左Drawable图标,当editTextView为 onFocus 时,我设法更改了可绘制图标,问题是当我尝试当editTextVw不为空时,保持onFocus Drawable为空,什么也没有发生,就像只有 setOnFocusChangeListener 正常工作一样。

  

这是来自测试EditTextView App的图像:

第一张图片是editTextView不在焦点上

the first image is when the editTextVw is not onFocus

第二张图片是editTextVw处于onFocus

the second image is when the editTextVw is onFocus

  

这是我在onFocus时用于更改可绘制对象的代码:

editTextVw2 = (EditText) findViewById(R.id.editTextVw2);
   editTextVw2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                Drawable focus_icon = context.getResources().getDrawable(R.drawable.user_focus_18dp); //the focus_icon image
                focus_icon = DrawableCompat.wrap(focus_icon);
                editTextVw2.setCompoundDrawablesWithIntrinsicBounds(focus_icon, null, null, null);
            } else {
                Drawable default_icon = context.getResources().getDrawable(R.drawable.user_18dp); //the default image
                default_icon = DrawableCompat.wrap(default_icon);
                editTextVw2.setCompoundDrawablesWithIntrinsicBounds(default_icon, null, null, null);
            }
        }
    });

-我试图添加一种方法来检查editTextVw是否不为空,然后再次应用

  Drawable focus_icon = context.getResources().getDrawable(R.drawable.user_focus_18dp); //the focus_icon image
            focus_icon = DrawableCompat.wrap(focus_icon);
            editTextVw2.setCompoundDrawablesWithIntrinsicBounds(focus_icon, null, null, null);

但它保持不变。

  

对此应该有什么快速解决方案?

4 个答案:

答案 0 :(得分:0)

您还需要在编辑文本中添加TextChangedListener

editTextVw2.addTextChangedListener(object : TextWatcher {
        override fun afterTextChanged(s: Editable?) {
            if (s.text.toString().isNotEmpty){
                //change color icon
            }

        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        }
    })

答案 1 :(得分:0)

很难理解您想要什么。 :)

让我假设当此视图处于焦点或不为空时,您需要更改EditView左图标的颜色。

因此,Google / Android团队没有提供任何快速操作。我在下面编写的完整的解决方案:

  • (1)首先,在创建新视图时检查其EditView状态。
  • (2)添加一个侦听器以检查视图焦点与否。
  • (3)如果允许此EditView的内容已通过编程方式更改。准备onTextChanged侦听器。

更新:按照您的源代码,我认为应该如此。

editTextVw2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        boolean shouldDefault = !hasFocus;
        if (shouldDefault) {
            shouldDefault = TextUltis.isEmpty(
                    editTextVw2.getText().toString());
        }

        Drawable icon = context.getResources().getDrawable(
                shouldDefault ? R.drawable.user_18dp : R.drawable.user_focus_18dp);
        editTextVw2.setCompoundDrawablesWithIntrinsicBounds(
                default_icon, null, null, null);
    }
});

答案 2 :(得分:0)

如果我正确理解,则应在if条件和else条件中更改代码,如下所示:

editTextVw2 = (EditText) findViewById(R.id.editTextVw2);
   editTextVw2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                   Drawable default_icon = context.getResources().getDrawable(R.drawable.user_18dp); //the default image
                default_icon = DrawableCompat.wrap(default_icon);
                editTextVw2.setCompoundDrawablesWithIntrinsicBounds(default_icon, null, null, null);
            } else {

             Drawable focus_icon = context.getResources().getDrawable(R.drawable.user_focus_18dp); //the focus_icon image
                focus_icon = DrawableCompat.wrap(focus_icon);
                editTextVw2.setCompoundDrawablesWithIntrinsicBounds(focus_icon, null, null, null);
            }
        }
    });

我希望能有所帮助, 否则请描述更多

答案 3 :(得分:0)

  

感谢您的帮助,使用此方法,我终于得到了我需要的东西

editTextVw2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                // if the edit text is on focus change the icon 
                Drawable focus_icon = context.getResources().getDrawable(R.drawable.user_focus_18dp); //the focus_icon image
                focus_icon = DrawableCompat.wrap(focus_icon);
                editTextVw2.setCompoundDrawablesWithIntrinsicBounds(focus_icon, null, null, null);

                // if the edit text is not on focus
            } else {
                // and the edit text is not empty, change the ion
                if (editTextVw2.getText().toString().trim().length() > 0) {
                    Drawable focus_icon = context.getResources().getDrawable(R.drawable.user_focus_18dp); //the focus_icon image
                    focus_icon = DrawableCompat.wrap(focus_icon);
                    editTextVw2.setCompoundDrawablesWithIntrinsicBounds(focus_icon, null, null, null);
                    // otherwise if it's empty, return the the icon the the default one
                } else if (editTextVw2.getText().toString().trim().isEmpty()) {
                    Drawable default_icon = context.getResources().getDrawable(R.drawable.user_18dp); //the default image
                    default_icon = DrawableCompat.wrap(default_icon);
                    editTextVw2.setCompoundDrawablesWithIntrinsicBounds(default_icon, null, null, null);
                }
            }
        }
    });
  

如果这是更好的方法,请不要犹豫告诉我