Android: How to turn off IME for an EditText?

自作多情 提交于 2019-12-07 16:55:55

问题


How do I turn off the IME-functionality of an EditText?

Or: How do I avoid the display of the IME-keyboard?

I have a layout where my special keyboard sits below the EditText so there's no need to show the IME. Please understand that I cannot implement my keyboard as IME as it is specific for this very EditText and using it in any other context would only cause problems.

I tried to use

getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

in the onCreate() of the activity, but that doesn't seem to do anything in this situation.


回答1:


Think I found a way to do it... subclass EditText and override onCheckIsTextEditor() to return false:

public class EditTextEx extends EditText {

    public EditTextEx(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override 
    public boolean onCheckIsTextEditor() {
        return false;
    }       
}

I've tested it and I can't get the soft keyboard to show at all.




回答2:


editText.setInputType(EditorInfo.TYPE_NULL);



回答3:


While trying to get it working I also tried:

inputField.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        }
    });

inputField.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent event) {
        getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        return false;
    }
});

Both get called, but neither hide the IME-pop-up.



来源:https://stackoverflow.com/questions/4131448/android-how-to-turn-off-ime-for-an-edittext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!