隐藏片段中EditText的键盘

时间:2016-09-28 12:08:24

标签: android android-fragments

我正在使用android studio。我在片段页面中有编辑文本,现在我想在EditText外部单击后隐藏键盘。我使用了下面的代码,但它没有用。

*

先谢谢

6 个答案:

答案 0 :(得分:2)

尝试将onFocusChangeListener设置为EditText。在onFocusChange方法中,您可以隐藏键盘,如下所示:

mEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean hasFocus) {
            InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(locationEt.getWindowToken(), 0);
        }
    });

答案 1 :(得分:1)

尝试通过以下函数中的活动。有用。

 public static void hideKeyboard(Activity activity) {
            // Check if no view has focus:
            View view = activity.getCurrentFocus();
            if (view != null) {
                InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }

答案 2 :(得分:1)

将此代码写入放置片段的Activity

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    View view = getCurrentFocus();
    boolean ret = super.dispatchTouchEvent(event);

    if (view instanceof EditText) {
            try {
                View w = getCurrentFocus();
                int scrcords[] = new int[2];
                w.getLocationOnScreen(scrcords);
                float x = event.getRawX() + w.getLeft() - scrcords[0];
                float y = event.getRawY() + w.getTop() - scrcords[1];

                if (event.getAction() == MotionEvent.ACTION_UP && (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w.getBottom())) {
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (getWindow() != null && getWindow().getCurrentFocus() != null) {
                        imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    return ret;
}

快乐的编码!

答案 3 :(得分:1)

在外部或其他视图点击事件时使用此方法

public void hideKeyboard(View view) {
    InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

就像这样

txtHeader.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
       hideKeyboard(txtHeader);
   }});

答案 4 :(得分:0)

请点击您的父级布局使用以下代码:

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);

答案 5 :(得分:0)

尝试这个,它的活动以及片段

public void setupUI(View view) {
    if (!(view instanceof EditText)) {
        view.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                //code of hide soft keyboard
                return false;
            }
        });
    }
    if (view instanceof ViewGroup) {
        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
            View innerView = ((ViewGroup) view).getChildAt(i);
            setupUI(innerView);
        }
    }
}