如何覆盖键盘开启按钮?

时间:2015-01-03 10:11:21

标签: android keyboard keycode

enter image description here

当用户在向下按钮上关闭键盘时,我想隐藏我的edittext。

尝试-1

我已经尝试过KeycodeBack,但这不起作用

 @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {


        MainActivity.editText2.setVisibility(View.INVISIBLE) ;

        return true;
    }

    return super.onKeyDown(keyCode, event);
}

尝试-2

我已经尝试过editorActionListener,但也没有帮助

editText2.setOnEditorActionListener(new TextView.OnEditorActionListener() {
           @Override
           public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
               boolean handled = false;

               if (actionId == EditorInfo.IME_ACTION_DONE ||event.getKeyCode() == KeyEvent.KEYCODE_BACK||event.getAction()==KeyEvent.KEYCODE_ENTER
                       ) {
MainActivity.editText2.setVisibility(VISIBLE);

               } 
               return handled ;
           }
       });

Event始终为null

1 个答案:

答案 0 :(得分:1)

在您的活动中,您可以捕捉此活动

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);


    // Checks whether a hardware keyboard is available
    if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
        Toast.makeText(this, "keyboard visible", Toast.LENGTH_SHORT).show();
    } else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
        Toast.makeText(this, "keyboard hidden", Toast.LENGTH_SHORT).show();
    }
}

在您的清单中,您应该在配置文件中添加这些更改。

    <activity
        android:configChanges = "keyboard|keyboardHidden" // and if you have any other config changes.