android EditText 的键盘弹出(不弹出)坑爹

需求:如果想要不弹出键盘并且失去光标,

在layout布局文件里,在EditText的父布局中加上两个属性(我是直接加载顶层控件上)

android:focusable="true"
android:focusableInTouchMode="true"
缺点:当你点击 EditText  的时候键盘弹不起来

解决方法:给EditText  添加点击事件

edit.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        SoftInputUtil.showKeyBoard(edit);
    }
});
 
  
public static void showKeyBoard(final EditText editText) {
    if (editText != null) {

        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                try {
                    InputMethodManager inputManager =
                            (InputMethodManager) editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    inputManager.showSoftInput(editText, 0);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }, 300);

    }
}

另一种方式:在布局里面多放一个edittext  ,用布局遮挡住, 让光标在它身上,然后关闭键盘  也可以实现;

你可能感兴趣的:(Android)