【问题标题】:Hide soft keyboard on activity where no keyboard operation found在未找到键盘操作的活动上隐藏软键盘
【发布时间】:2014-04-03 07:29:10
【问题描述】:

我有一个选项卡视图,每个选项卡有一个 Activity,当我从第一个选项卡(仅显示带有搜索(编辑文本)的列表)切换到具有 TextView 的第二个选项卡时,软键盘仍然那里。我想让它消失。有关如何解决此问题的任何建议?

【问题讨论】:

    标签: android textview android-softkeyboard android-keypad


    【解决方案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);

    【讨论】:

      【解决方案2】:

      试试这个

      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);
          }
      }
      

      【讨论】:

        【解决方案3】:

        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);
        

        【讨论】:

        • 你把这个方法放在哪里
        • 我在课堂上放了这个
        • 实际上,只要您进入此活动,您的编辑文本就会成为焦点,如果您想通过默认关闭它,然后查看我编辑的答案
        • 我将此代码粘贴到第一个活动中,我有 listview 和 edittext...或者在第二个活动中我只使用了 textview
        • 粘贴到你不想软键盘的地方
        【解决方案4】:

        只要你触摸你定义的字段以外的地方,键盘就会消失。

        //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;
            }
        

        【讨论】:

          【解决方案5】:

          给你一个答案,它会起作用的

          yourtextview.setRawInputType(Configuration.KEYBOARDHIDDEN_YES);
          

          【讨论】:

            猜你喜欢
            • 2016-02-01
            • 1970-01-01
            • 2011-02-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-07-23
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多