Disable Software Keyboard in Android until EditText is chosen

烂漫一生 提交于 2019-12-04 09:49:00

问题


how can I prevent the software keyboard to disappear until a specific EditText is choosen? I have several EditTexts in my Layout when it is opened the first is automatically selected and the software keyboard is shown.

I know i can disable this by setting android:focusable="false". But then it is impossible to click on the item and get the item displayed.

What I want: The activity is started and the user sees all EditTexts, then he clicks on one and the software keyboard opens and stuff can be entered in the EditText. Is this possible?


回答1:


In your activity onCreate()

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



回答2:


I try this to make the keyboard appear only when user clicks on edittext, all the other solutions don't work for me. It's a bit weird but from now it's the solution I found. You must put this events in all edittexts on the layout.

OnClickListener click = new OnClickListener() {
    @Override
    public void onClick(View v) {
        v.setFocusableInTouchMode(true);
        v.requestFocusFromTouch();
    }
};
OnFocusChangeListener focus = new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (!hasFocus) {
            v.setFocusableInTouchMode(false);
        }
    }
};

Like this:

EditText text = (EditText) getActivity().findViewById(R.id.myText);
text.setText("Some text");
text.setFocusableInTouchMode(false);
text.setOnClickListener(click); 
text.setOnFocusChangeListener(focus);

EDIT:

I just make a custom edit text to make easy to use in my project, hope it was usefull.

public class EditTextKeyboardSafe extends EditText {
    public EditTextKeyboardSafe(Context context) {
        super(context);
        initClass();
    }

    public EditTextKeyboardSafe(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
        initClass();
    }

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

    private void initClass() {
        this.setFocusableInTouchMode(false);
        this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setFocusableInTouchMode(true);
                v.requestFocusFromTouch();
            }
        });
        this.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    v.setFocusableInTouchMode(false);
                }
            }
        });
    }

}



回答3:


final InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

    txt1.setOnFocusChangeListener(new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(!(hasFocus || txt2.hasFocus()))
            {   
            mgr.hideSoftInputFromWindow(txt1.getWindowToken(), 0);
            }

        }
    });

this code has good effect for focus handling and keyboard display...



来源:https://stackoverflow.com/questions/7009096/disable-software-keyboard-in-android-until-edittext-is-chosen

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