EditText文本输入框使用完毕自动掩藏输入法

很多时候EditText文本输入框在输入信息完成操作之后后,我们希望能够自动收缩掩藏起虚拟键盘,以方便处理其他业务逻辑。

比喻本文中EditText文本在输入商品编号完成商品检索之后,需要自动掩藏虚拟键盘,不要遮挡其他页面操作信息,当下次再检索商品信息时,再自动弹出虚拟键盘完成输入。

看上图效果,晚上输入操作之后,自动掩藏了输入法,下次使用时再打开。

关键代码如下:

EditText etxt_Index_QueryProduct = findViewById(R.id.etxt_Index_QueryProduct);
/**
 * 查询商品信息
 * Author:William(徐威)
 * Create Time:2018-08-29
 */
private void searchProduct() {
    try {
        String productId = etxt_Index_QueryProduct.getText().toString();
        if (productId != null && productId.length() > 0) {
            bindCategory();
            vpCategory.setCurrentItem(categoryList.size() - 1);
            
            //点击查询之后自动掩藏输入法
            InputMethodManager imm = ( InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm.isActive()) {
                imm.hideSoftInputFromWindow( etxt_Index_QueryProduct.getWindowToken(), 0 );
            }
        } else {
            Toast.makeText(this, "请输入商品编号之后再点击查询。", Toast.LENGTH_LONG).show();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

猜你喜欢

转载自blog.csdn.net/xuwei_net/article/details/82221497