Android如何从复合图标隐藏软键盘

4

我有一个带有复合图标的EditText,它位于右侧。当我按下该图标并清除文本时,我想隐藏软键盘。为此,我有以下代码:

        filterText.setOnTouchListener(new OnTouchListener() { 
        public boolean onTouch(View v, MotionEvent event) { 
                if (filterText.getCompoundDrawables()[2] == null) { 
                        // cross is not being shown so no need to handle 
                        return false; 
                } 
                if (event.getAction() != MotionEvent.ACTION_DOWN) { 
                        // only respond to the down type 
                        return false; 
                } 
                if (event.getX() > filterText.getMeasuredWidth() - 
                        filterText.getPaddingRight() - d.getIntrinsicWidth()) { 
                        filterText.setText(""); 
                        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                        return false; 
                } 
                else {
                    return true; 
                } 
        } 
    }); 

但它不起作用,因为editText似乎保持了焦点。我已经尝试过filterText.clearFocus但没有用。谢谢
1个回答

1
只是一个建议:你不能把一个 Button 放在 EditText 的右边吗?

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