在没有找到键盘操作的活动上隐藏软键盘

时间:2014-04-03 07:29:10

标签: android textview android-softkeyboard android-keypad

我有一个标签视图,每个标签有一个活动,当我从第一个标签切换到第二个标签时,该标签只显示带搜索的列表(编辑文本),第二个标签有一个TextView,软键盘仍然是那里。我希望它消失。关于如何解决这个问题的任何建议?

5 个答案:

答案 0 :(得分:1)

试试这个,android:configChanges="orientation|keyboardHidden"

       <activity
            android:name="YourActivityname"
            android:configChanges="orientation|keyboardHidden"
            android:screenOrientation="landscape"
            android:theme="@style/Theme.MyAwesomeTheme" >
        </activity>

另一种方法是强迫他们隐藏,如下所示。

将此代码放在utils类中以保持代码的有序性。

public static void hideKeyboard(Activity activity) {
        try {
            InputMethodManager inputManager = (InputMethodManager) activity
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.hideSoftInputFromWindow(activity.getCurrentFocus()
                    .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        } catch (Exception e) {
            // Ignore exceptions if any
            Log.e("KeyBoardUtil", e.toString(), e);
        }
    }

您可以将此方法称为Utils.hideKeyboard(your activity.this);

答案 1 :(得分:0)

试一试

public static void hideSoftInput(View _v,Context _c){
    if(_v.getWindowToken() != null){
        InputMethodManager inputManager = (InputMethodManager) _c.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(_v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
}

答案 2 :(得分:0)

jusr将此方法粘贴到您的类中,这将触摸方法和软键盘隐藏

@Override
    public boolean dispatchTouchEvent(MotionEvent event) {

        View v = getCurrentFocus();
        boolean ret = super.dispatchTouchEvent(event);

        if (v instanceof EditText) {
            View w = getCurrentFocus();
            int scrcoords[] = new int[2];
            w.getLocationOnScreen(scrcoords);
            float x = event.getRawX() + w.getLeft() - scrcoords[0];
            float y = event.getRawY() + w.getTop() - scrcoords[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);
                imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
            }
        }
    return ret;
    }

把它隐藏起来

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

答案 3 :(得分:0)

只要您触摸除定义的字段以外的其他键盘,键盘就会消失。

//to hide keyboard
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {

        View v = getCurrentFocus();
        boolean ret = super.dispatchTouchEvent(event);

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

            Log.d("Activity", "Touch event "+event.getRawX()+","+event.getRawY()+" "+x+","+y+" rect "+w.getLeft()+","+w.getTop()+","+w.getRight()+","+w.getBottom()+" coords "+scrcoords[0]+","+scrcoords[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);
                imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
            }
        }
    return ret;
    }

答案 4 :(得分:0)

你的一行答案是可行的

yourtextview.setRawInputType(Configuration.KEYBOARDHIDDEN_YES);