简体   繁体   中英

Disable back button when Soft Keyboard pops up, Android

I shifted the whole layout of my page by 250 when user inputs on the two EditText fields, and I need to shit it back when the keyboard is dismissed, I used

public class DoneOnEditorActionListener implements OnEditorActionListener {

@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    // TODO Auto-generated method stub
    try{
        if (actionId == EditorInfo.IME_ACTION_DONE 
                || actionId == EditorInfo.IME_NULL
                || event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
            InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);

            LoginActivity.rootView.setY(0);
            return true;    
        }
    }catch (Exception e){

    }

    return false;
}

} And it works fine, but when I click the back button, the keyboard is also dismissed and the layout is not shifted back. Is there a way to disable the back button only when the soft keyboard is up?

set a variable when soft keyboard is up ( how to check visibility of software keyboard in android? ) and check it in your overridden onBackPressed() method. If keyboard is up, do nothing, otherwise call the super method.

private boolean isKeyboardUp;

@Override
public void onBackPressed()
    {
        if(isKeyboardUp)
          //do nothing
        else
          super.onBackPressed();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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