关于android:如何覆盖键盘ondown按钮?

How to override the keyboard ondown button?

enter image description here

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

试试-1

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

1
2
3
4
5
6
7
8
9
10
11
12
 @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,但也没有用

1
2
3
4
5
6
7
8
9
10
11
12
13
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
2
3
4
5
6
7
8
9
10
11
12
@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();
    }
}

在您的Manifest中,您应该在configchages中添加这些更改。

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