Android软键盘只有在方向改变时才会消失

Android软键盘只有在方向改变时才会消失,android,keyboard,Android,Keyboard,只是想知道在应用程序从EditText收集输入后,如何让软键盘立即消失。现在,“保存”按钮收集文本并成功地将EditText中的字符串设置回空白,但软键盘在方向切换之前不会消失。我试图通过编程隐藏软键盘,但它不起作用 下面是保存按钮代码: private OnClickListener saveButtonListener = new OnClickListener() { @Override public void onClick(View v) { i

只是想知道在应用程序从EditText收集输入后,如何让软键盘立即消失。现在,“保存”按钮收集文本并成功地将EditText中的字符串设置回空白,但软键盘在方向切换之前不会消失。我试图通过编程隐藏软键盘,但它不起作用

下面是保存按钮代码:

    private OnClickListener saveButtonListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        if (newListEditText.getText().length() > 0) {  //Make sure the user actually wrote something
            addStore(newListEditText.getText().toString());  //Adds a button to view with name from EditText
            newListEditText.setText("");
            ((InputMethodManager) getSystemService(
                       Context.INPUT_METHOD_SERVICE)).hideSoftInputFromInputMethod(
                            newListEditText.getWindowToken(), 0);  //Supposed to hide soft keyboard but doesn't do it
我还尝试将代码[android:imeOptions=“actionDone”]插入到xml布局文件中,正如在对类似问题的回答中所建议的那样,但没有任何改变

提前感谢您的输入

只要替换这个:

Context.INPUT_METHOD_SERVICE)).hideSoftInputFromInputMethod(  
为此:

Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(

请使用此代码执行此操作

InputMethodManager ipmm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
ipmm.hideSoftInputFromWindow(url.getWindowToken(), 0);
您可以忽略url.getWindowToken()并将其替换为“null”

仅供参考-在我的案例/代码中,我使用url作为通过editText接收的字符串,例如

inputText = (EditText) findViewById (R.id.edittext1); 
String url = inputText.getText().toString();

试试这段代码,所以每当用户触摸屏幕的其他部分时,除了编辑文本, 软键盘将消失

所以,在您的情况下,如果用户完成了文本并尝试触摸按钮,这将起作用

    @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;
}
@覆盖
公共布尔dispatchTouchEvent(MotionEvent){
视图v=getCurrentFocus();
布尔ret=super.dispatchTouchEvent(事件);
if(v instanceof EditText){
视图w=getCurrentFocus();
int scrcoords[]=新int[2];
w、 getLocationOnScreen(scrcoords);
float x=event.getRawX()+w.getLeft()-scrcoords[0];
float y=event.getRawY()+w.getTop()-scrcoords[1];
Log.d(“活动”,
“触摸事件”+event.getRawX()+“,“+event.getRawY()
+“+x+”,“+y+”矩形“+w.getLeft()+”,“
+w.getTop()+“,+w.getRight()+”,“
+w.getBottom()+“coords”+scrcoords[0]+,”
+scrcoords[1]);
如果(event.getAction()==MotionEvent.ACTION\u UP
&&(x=w.getRight()| | yw
.getBottom()){
InputMethodManager imm=(InputMethodManager)getSystemService(Context.INPUT\u方法\u服务);
imm.hideSoftInputFromWindow(getWindow().getCurrentFocus())
.getWindowToken(),0);
}
}
返回ret;
}

您是否尝试了
切换软输入(InputMethodManager.HIDE_IMPLICIT_ONLY,0)
而不是
隐藏输入fromInputMethod(EditText,0)
?。同时检查用户184994的答案(就在下面)。我认为他是对的。这一个也可以工作。InputMethod和…Window之间的区别是什么?