Android:触摸EditText时隐藏软键盘

时间:2016-06-08 06:43:51

标签: android android-alertdialog android-keypad

按下“返回”按钮或“完成”按钮时隐藏了softInputKeyboard的操作系统行为,我想在用户单击EditText时隐藏键盘。所以我创建了一个BaseActivity并在其中编写了以下代码。

    public class BaseActivity extends AppCompatActivity {

    @Override
    public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        boolean handleReturn = super.dispatchTouchEvent(ev);

        View view = getCurrentFocus();

        int x = (int) ev.getX();
        int y = (int) ev.getY();

        if (view instanceof EditText) {
            EditText innerView = (EditText) getCurrentFocus();

            if (ev.getAction() == MotionEvent.ACTION_UP &&
                    !getLocationOnScreen(innerView).contains(x, y)) {

                InputMethodManager input = (InputMethodManager)
                        getSystemService(Context.INPUT_METHOD_SERVICE);
                input.hideSoftInputFromWindow(getWindow().getCurrentFocus()
                        .getWindowToken(), 0);
            }
        }

        return handleReturn;
    }

    protected Rect getLocationOnScreen(EditText mEditText) {
        Rect rect = new Rect();
        int[] location = new int[2];

        mEditText.getLocationOnScreen(location);

        rect.left = location[0];
        rect.top = location[1];
        rect.right = location[0] + mEditText.getWidth();
        rect.bottom = location[1] + mEditText.getHeight();

        return rect;
    }
}

为MyActivity扩展了上面的类,它可以在EditTexts的布局上正常工作。但是当我弹出一个自定义布局的AlertDialog并且里面有很多EditText时,它无效。

我触发了AlertDialog中的EditText,甚至是Dialog的前景,但是键盘没有被解雇。

如何在这种情况下隐藏?

2 个答案:

答案 0 :(得分:0)

使用以下代码。它对我有用

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        View v = getCurrentFocus();
        if ( v instanceof EditText) {
            Rect outRect = new Rect();
            v.getGlobalVisibleRect(outRect);
            if (!outRect.contains((int)event.getRawX(), (int)event.getRawY())) {
                v.clearFocus();
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
            }
        }
    }
    return super.dispatchTouchEvent(event);
}

答案 1 :(得分:0)

要实现这一目标需要记住两件事 -

假设xml结构类似于

<ViewGroup focusable=true focusInTouchMode=true>
     <EditText />
</ViewGroup>
  1. 将父视图组设为 focusable = true&amp; focusInTouchMode =真
  2. 在edittext上设置onfocuschangelistener(),即

    edittext.editCaption.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }
    });