Android软键盘弹出时禁用后退按钮

时间:2013-10-15 06:46:10

标签: java android

当用户输入两个EditText字段时,我将页面的整个布局移动了250,并且当键盘被解除时我需要把它拉回来,我用了

public class DoneOnEditorActionListener实现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;
}

} 它工作正常,但当我单击后退按钮时,键盘也被解除,布局不会向后移动。有没有办法只在软键盘启动时禁用后退按钮?

1 个答案:

答案 0 :(得分:1)

在软键盘启动时设置变量(how to check visibility of software keyboard in android?)并在覆盖的onBackPressed()方法中进行检查。如果键盘已启动,则不执行任何操作,否则请调用super方法。

private boolean isKeyboardUp;

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