简体   繁体   English

如何防止键盘在EditText onTouch中显示?

[英]How to prevent keyboard to show in EditText onTouch?

What I want to do is this: I have an EditText and when the user click on it, the user can move the caret position, same as the EditText, but without showing the keyboard. 我想要做的是:我有一个EditText,当用户单击它时,用户可以移动插入号位置,与EditText相同,但不显示键盘。

I've tried with setInputType(0); 我已经尝试过setInputType(0); and it hides completly the keyboard but the cursor doesn't appears. 并且它完全隐藏了键盘,但没有出现光标。

Is there any way to do this? 有什么办法吗?

Thank's 谢谢

The following tricks works for me. 以下技巧对我有用。 Caret and soft keyboard both active onTouch event of editText. 插入符号和软键盘均激活editText的onTouch事件。 So Call touch event and then hide the keyboard manually. 因此,呼叫触摸事件,然后手动隐藏键盘。

myEditText.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        final boolean ret = dialerNumber.onTouchEvent(event);
        final InputMethodManager imm = ((InputMethodManager) myContext
                .getSystemService(Context.INPUT_METHOD_SERVICE));
        try{
            imm.hideSoftInputFromWindow(myEditText.getApplicationWindowToken(), 0);
        }catch(Exception e){
            e.printStackTrace();
        }
        return ret;
});

我没有尝试完全抑制键盘,但是在使用InputMethodManager.hideSoftInputFromWindow方法之前,我已经手动隐藏了软键盘。

这样行吗?

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM