【问题标题】:How to override the keyboard ondown button?如何覆盖键盘的ondown按钮?
【发布时间】:2015-01-03 10:11:20
【问题描述】:

当用户在向下按钮上关闭键盘时,我想隐藏我的编辑文本..

试试 -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

【问题讨论】:

标签: android keyboard keycode


【解决方案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();
    }
}

并且在您的清单中,您应该在 configchages 中添加这些更改。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-15
    • 1970-01-01
    相关资源
    最近更新 更多