Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
173 views
in Technique[技术] by (71.8m points)

Prevent softkeyboard dismiss on the android back button press

I have an activity with an edittext. when the activity comes, i always focusing that Edittext. i made the softkeyboard always visible by giving

android:windowSoftInputMode="stateAlwaysVisible" 

in the manifest. i m doing the functionality on the done button press of the softkeyboard. Actually my need is the softkeyboard should always present when the user is on this activity. now i disabled the back button press, by overriding and do nothing.

@Override
public void onBackPressed() {
    // Do nothing
}

but softkeyboard dismisses on the back press. how to get rid of this. I had tried this Prevent soft keyboard from being dismissed but still on the back press keyboard get dismisses. But it comes back by again pressing the back button, My need is backpress should not dismiss the softkeyboard. Any help will be appreciated.

question from:https://stackoverflow.com/questions/27721709/prevent-softkeyboard-dismiss-on-the-android-back-button-press

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

create one custom EditText like follow with one interface:

public class CustomEditText extends EditText {


    public void setHandleDismissingKeyboard(
        handleDismissingKeyboard handleDismissingKeyboard) {
           this.handleDismissingKeyboard = handleDismissingKeyboard;
    }

    private handleDismissingKeyboard handleDismissingKeyboard;

    public interface handleDismissingKeyboard {
        public void dismissKeyboard();
    }

    @SuppressLint("NewApi")
    public CustomEditText(Context context, AttributeSet attrs,
                             int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        // TODO Auto-generated constructor stub
    }

    public CustomEditText(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public CustomEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // TODO Auto-generated constructor stub
    }


    @Override
    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_UP) {
            handleDismissingKeyboard.dismissKeyboard();
            return true;
        }
        return super.dispatchKeyEvent(event);
    }

then in your activity create initialize your CustomEditText and use:

customEditText.setHandleDismissingKeyboard(this);

then implement class and override method and put your code in that

for more info about my answer all thing you need is onKeyPreIme, you can override that in your EditText class ( as i post for you ) to handle all key on that, like back Key or any other keyboard key, I put one inner interface in custom class to get call back from this class to each activity or class that you want, you can do that with static method too, and as you want prevent from dismissing keyboard you can just return true in that.

for using CustomEditText you can use xml or in java, define that and initialize that like other widget ( Button , TextView , ... ), and only different is you need define this in your xml like :

 <yourPackage.CustomEditText ..... />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...