【问题标题】:Cannot hide Android soft keyboard even with inputmanager即使使用输入管理器也无法隐藏 Android 软键盘
【发布时间】:2015-09-28 23:57:48
【问题描述】:

问题:

我想在按下“添加”按钮时隐藏键盘。屏幕上有两个EditText。键盘在启动 Activity 时不会出现,这很好,但在单击按钮时它不会消失。

以下是我看到的 Stack Overflow 中所有可能的问题,但它们的答案对我没有帮助:

Close/hide the Android Soft Keyboard

Programmatically Hide/Show Android Soft Keyboard

How to hide Soft Keyboard when activity starts

How to hide soft keyboard on android after clicking outside EditText?

还有很多其他的。

这是我的代码:

添加活动

public class AddActivity extends ActionBarActivity {
EditText text1,text2;
DbHelper db;
ListView l;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add);
    db = new DbHelper(this);
    l = (ListView) findViewById(R.id.listInAddActivity);
    text1 = (EditText) findViewById(R.id.i1);
    text2 = (EditText) findViewById(R.id.i2);
//        text1.setInputType(InputType.TYPE_NULL);
  //      text2.setInputType(InputType.TYPE_NULL);
    hideKeyboard();

    loadDataInAdd();

}
public void addNewTask(View view) {
    String s1 = text1.getText().toString();
    String s2 = text2.getText().toString();
    db.addData(s1,s2);
    loadDataInAdd();
    hideKeyboard();
}
public void loadDataInAdd()
{
    try {
        Cursor cursor = db.fetchData();

        ListAdapter myAdapter = new SimpleCursorAdapter(this, R.layout.tasks,
                cursor,
                new String[]{db._ID, db.COLUMN_1, db.COLUMN_2},
                new int[]{R.id.idnum, R.id.c1, R.id.c2}, 0);
        l.setAdapter(myAdapter);
    }
    catch(NullPointerException e)
    {
        e.printStackTrace();
    }
  //  MainActivity.loadData();
}
private void hideKeyboard() {
    // Check if no view has focus:
    View view = this.getCurrentFocus();
    if (view != null) {
        InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
}

}

大多数答案都是关于InputManager 方法,但它对我不起作用,即单击按钮时不会隐藏键盘。这是我使用的InputManager 的另一种变体:

View view = this.getCurrentFocus();
    if (view != null) {
        InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0);

我也试过这个:

getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN
);

这也不起作用(点击按钮时键盘不会隐藏)。

我也试过了:

     <activity android:name=".AddActivity"
              android:label="@string/app_name"
              android:parentActivityName=".MainActivity"
        android:windowSoftInputMode="stateAlwaysHidden">

    </activity>

   <activity android:name=".AddActivity"
              android:label="@string/app_name"
              android:parentActivityName=".MainActivity"
        android:windowSoftInputMode="stateAlwaysHidden">

    </activity>

有了这个,我的应用程序停止了工作:

   InputMethodManager inputManager = (InputMethodManager)
            getSystemService(Context.INPUT_METHOD_SERVICE);

    inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
            InputMethodManager.HIDE_NOT_ALWAYS);

这个完全隐藏了键盘(即使点击了editText,键盘也没有出现):

text1.setInputType(InputType.TYPE_NULL);
text2.setInputType(InputType.TYPE_NULL);

【问题讨论】:

    标签: android keyboard


    【解决方案1】:

    我决定为我的按钮使用onclicklistener,而不是使用另一个函数并通过onClickAddActivity.xml 中调用它。

    当我决定再次尝试使用OnCliCkListener 时,我的问题已经问到一半了。经过几次随机尝试,以下是适合我的最终代码:

    btn.setOnClickListener(new View.OnClickListener() {
    
    
            public void onClick(View v) {
                // TODO Auto-generated method stub
    
                    InputMethodManager inputManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                    inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    
            }
        });
    

    另一种方法是按以下方式使用OnClickListener

        private View.OnClickListener mListener = new View.OnClickListener() {
        public void onClick(View v) {
            // do something when the button is clicked
    
            //public void onClick(View v) {
    
                try {
                    InputMethodManager inputManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                    inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
                } 
     catch (Exception e) {
          e.printStackTrace();
                }
    
        }
    };
    

    对像我这样的初学者来说很重要:

    • 以上代码应放在protected void onCreate(Bundle savedInstanceState)之前
    • 要调用上述代码,请将此代码放入protected void onCreate(Bundle savedInstanceState)

      btn.setOnClickListener(mListener);

    • 在两种方法中都使用try-catch 进行异常处理(onClickListenersetOnClickListener

    在搜索为什么onClickXML 属性对我不起作用,而OnClickListener 对我起作用时,我发现以下链接很有用:

    setOnclickListener vs OnClickListener vs View.OnClickListener

    Android onClick in XML vs. OnClickListener

    How exactly does the android:onClick XML attribute differ from setOnClickListener?

    Difference between OnClick() event and OnClickListener?

    现在,在我的回答进行到一半时,我意识到我在使用 XML 中的 onClick 时犯了错误。这是我为摆脱 OnClickListeners 所做的:

    • 首先,我将隐藏键盘的代码部分移到onClick 中的方法中。在我的例子中,我将hideKeyboard() 中的整个代码移动到addNewTask

    关于使用onClick的一些要点

    • 方法应该是publicvoid

    • 它应该带一个View 参数,例如View V

    最后,有效的代码:

      public void addNewTask(View view) {
        String s1 = text1.getText().toString();
        String s2 = text2.getText().toString();
        db.addData(s1, s2);
        loadDataInAdd();
      //  hideKeyboard(); below is the code to hide keyboard
        if (view != null) {
            InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }
    

    总结

    我找到了 3 种隐藏软键盘的方法,使用:

    • OnClickListener:

      private View.OnClickListener mListener = new View.OnClickListener() {
      public void onClick(View v) {
          // do something when the button is clicked
      
          //public void onClick(View v) {
      
              try {
                  InputMethodManager inputManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                  inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
              } 
      
         catch (Exception e) {
        e.printStackTrace();
              }
      
      }
      };
      
    • setOnClickListener:

        btn.setOnClickListener(new View.OnClickListener() {
      
      
          public void onClick(View v) {
              // TODO Auto-generated method stub
      
                  InputMethodManager inputManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                  inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
      
          }
      });
      
    • onClick,XML 属性:

      public void addNewTask(View view) {
      String s1 = text1.getText().toString();
      String s2 = text2.getText().toString();
      db.addData(s1, s2);
      loadDataInAdd();
      //  hideKeyboard();
      if (view != null) {
          InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
          inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
      }
      }
      

    【讨论】:

    • 感谢您的回答。但所有答案都已经可用。如果您在 Google 上进行更好的搜索。但没关系,大家都在一起。
    • @MD:你可能是对的,因为我是 Android 的初学者,所有的部分答案让我很困惑。在过去的几天里,我浏览了数百个链接(大部分来自 SO),并试图实现所有的答案。但他们只是不适合我的情况。例如,InputMethodManager 是要走的路,几乎所有的答案都暗示了这一点,但你可以在我的问题中看到它是如何失败的。我必须拼凑这个答案的一部分,那个答案的一部分,我自己的逻辑的一部分,什么代码应该去哪里,等等。
    • 是的,这就是我写appreciated your answer的原因。 +1 向上
    【解决方案2】:

    这将帮助您在启动时隐藏键盘,直到您触摸编辑文本。

    getWindow().setSoftInputMode(
            WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN
        );
    

    在你的情况下,试试这个

    btn.setOnClickListener(new View.OnClickListener() {
    
    
            public void onClick(View v) {
                 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
    
            }
        });
    

    【讨论】:

      【解决方案3】:

      看看这个

      View view = this.getCurrentFocus();
      if (view != null) {  
          InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
          imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
      }
      

      Close/hide the Android Soft Keyboard

      【讨论】:

        猜你喜欢
        • 2014-04-07
        • 2019-02-22
        • 2011-02-17
        • 2016-05-04
        • 2012-05-18
        • 2017-06-07
        • 1970-01-01
        • 2012-01-20
        • 2012-10-30
        相关资源
        最近更新 更多