Android如何让EditText不弹出键盘

适用于需要在Edittext上添加一个popupwindow的情况(比如做一个日期选择器而不是手动输入)

同一页面的Edittext重写。添加以下方法:


this.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (!hasFocus) {
            Tools.closeInputMethod(getContext(), input);
        }
    }
});
public static void closeInputMethod(Context context, EditText editText) {
    InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    boolean isOpen = imm.isActive();
    if (isOpen) {
        imm.hideSoftInputFromWindow(editText.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
}


弹出popupwindow的那个edittext给一下focus:


input.setFocusable(true);
input.requestFocus();
input.setFocusableInTouchMode(false);
input.setInputType(InputType.TYPE_NULL);
final EditText editText = input;
input.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        picker.show();
        //强制获取焦点
         editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
        editText.setFocusableInTouchMode(false);
    }
});



你可能感兴趣的:(实战记录)