即使使用 inputmanager 也无法隐藏 Android 软键盘

Cannot hide Android soft keyboard even with inputmanager

问题:

I want to hide the keyboard when the button "Add" is pressed. There are two EditText on the screen. The keyboard doesn't appear at starting the activity, which is good, but it doesn't go away on clicking button.

以下是我从 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);

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

当我决定再次尝试使用 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进行异常处理(onClickListener,setOnClickListener)

在搜索 onClick 的原因时,XML 属性对我不起作用,而 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 种隐藏软键盘的方法,使用:

  • 点击监听器:

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

看看这个

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

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

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

        }
    });