在Android中,是否有一种方法可以在单击按钮后隐藏虚拟键盘?当用户触摸EditText组件时,键盘最初会弹出;我希望在按下按钮后它能关闭。
隐藏键盘的最佳实践:
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
它会自动获取当前焦点并隐藏键盘。无论你有多少个EditText
视图。使用以下代码
your_button_id.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
// TODO: handle exception
}
}
});