2016-09-22 204 views
0

我的主要活动有2个EditText(用户名和密码)和一个按钮(登录)。我在按钮下方显示状态。Android:在按钮上隐藏键盘按

当用户点击按钮时,我需要隐藏键盘,否则键盘会出现状态信息。

所以在登录按钮的点击事件,我用下面的代码编程关闭键盘:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); 

我相信上面的代码,如果用于切换键盘状态,而不是专门针对关闭它。

现在的问题是,如果键盘已折叠并且用户单击该按钮,则会显示键盘。

我需要一个替代上述代码,它将检查键盘是否关闭,如果关闭打开它。

+0

小号查询问答,在最上面。 http://stackoverflow.com/a/1109108/6525469 –

+0

谢谢,sharma bhai –

回答

0

试试这个:

public static final void hideKeyboard(View view, Context context) { 
    InputMethodManager inputMethodManager =(InputMethodManager)context.getSystemService(Activity.INPUT_METHOD_SERVICE); 
    inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0); 
} 

鉴于发送您的按钮:

hideKeyboard(mButtonTest,context); 
1

收银台下面的代码

/** 
* Hides the soft keyboard 
*/ 
public void hideSoftKeyboard() { 
    if(getCurrentFocus()!=null) { 
     InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); 
      inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); 
    } 
} 


/** 
* Shows the soft keyboard 
*/ 
public void showSoftKeyboard(View view) { 
    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); 
    view.requestFocus(); 
    inputMethodManager.showSoftInput(view, 0); 
} 
0

尝试:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);