如何在程序中以编程方式隐藏右侧的可绘制资源

8

我正在为EditText设置右侧的drawable,像下面这样:

editText.setCompoundDrawablesWithIntrinsicBounds(null, null, ContextCompat.getDrawable(getApplicationContext(), R.drawable.ic_clear_black_24dp), null);

我在XML中为EditText设置可绘制的左侧。我想通过编程方式将其可见性设置为显示或隐藏。如何实现。

我在搜索时使用EditText。当开始输入时,我会通过程序设置清除图标。

清除图标将清除EditText中的文本。当单击没有文本的清除图标时,我希望关闭键盘并使清除图标隐藏。 以下是我的代码,

 editText.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    editText.setCursorVisible(true);
                    editText.setCompoundDrawablesWithIntrinsicBounds(null, null, ContextCompat.getDrawable(getApplicationContext(), R.drawable.ic_clear_black_24dp), null);
                }
            });
            editText.setOnTouchListener(new View.OnTouchListener() {
                @SuppressLint("ClickableViewAccessibility")
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if(event.getAction() == MotionEvent.ACTION_UP) {
                        if(editText.getCompoundDrawables()[2]!=null){
                            if(event.getX() >= (editText.getRight()- editText.getLeft() - editText.getCompoundDrawables()[2].getBounds().width())) {

                                if(!editText.getText().toString().equals("")) {

                                    editText.setText("");
                                }
                                else {

                                   // getWindow().setSoftInputMode(
                                     //       WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
                                    editText.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
                                    closeKeyboard();
                                    editText.setCursorVisible(false);
                                }
                            }
                        }
                    }
                    return false;
                }
            });

我希望您能够以编程方式隐藏它。

你尝试过在同一位置设置null来清除吗? - Shadow Droid
这样不起作用,因为您在onClick中又设置了drawable。而且在onTouch中,您已经返回false,因此会调用onClicked。 - Shadow Droid
@ShadowDroid 我想要达到的目标是:在使用清除图标搜索文本后,我可以清除文本。当单击清除图标并清空EditText时,我还希望通过关闭键盘和更改可绘制对象的可见性来关闭搜索。 - Kousalya
4个回答

5

setCompoundDrawablesWithIntrinsicBounds()方法中传递null,即可隐藏你的editText中的Drawable图标。

示例代码:

editText.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);

@Kousalya 尝试传递 0 而不是 null - AskNilesh
1
@Kousalya 和 @NileshRathod,null 工作正常,我刚试过了。你只需要执行 editText.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null)。请确保您没有重新加载布局? - Shadow Droid

3
尝试
editText.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);

3
请将您的onTouchListener更改为以下内容。不要寻找ACTION_UP,而是选择ACTION_DOWN,因为它在onTouch中首先被调用,从那里将return true,现在我们不需要在关闭键盘时进行onClick操作。
editText.setOnTouchListener(new View.OnTouchListener() {
                @SuppressLint("ClickableViewAccessibility")
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if(event.getAction() == MotionEvent.ACTION_DOWN) {
                        if(editText.getCompoundDrawables()[2]!=null){
                            if(event.getX() >= (editText.getRight()- editText.getLeft() - editText.getCompoundDrawables()[2].getBounds().width())) {

                                if(!editText.getText().toString().equals("")) {

                                    editText.setText("");
                                }
                                else {

                                   // getWindow().setSoftInputMode(
                                     //       WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
                                    editText.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
                                    closeKeyboard();
                                    editText.setCursorVisible(false);
                                    return true;
                                }
                            }
                        }
                    }
                    return false;
                }
            });

谢谢,现在它可以工作了。但是现在我面临不同的问题。 - Kousalya
@Kousalya,请问这个解决方案引起了什么问题? - Shadow Droid
不是那样的...不是因为那个解决方案...当清除图标被点击后,我关闭键盘,然后在EditText被点击时再次打开。我的键盘只在第二次点击时打开。我不知道为什么。 - Kousalya

2

清空右侧的可绘制对象

editText.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);

这里的第三个参数是正确的可绘制对象。

或者你可以使用

editText.setCompoundDrawables(null,null, null, null);

这里

setCompoundDrawables(left, top, right, bottom)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接