Android键盘打开或关闭监听器事件无法正常工作

时间:2017-06-14 09:32:27

标签: android android-layout

我正试图在Android上的键盘关闭上做一些操作。为此,我尝试了如下

@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();
    }
}

但我的活动未触发此configurationNotChanged。怎么了?

我们还有其他办法处理键盘打开/关闭吗?

4 个答案:

答案 0 :(得分:1)

InputMethodManager imm =  ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE));
    if (imm.isAcceptingText()) {
        Log.e("TAG","VISIBLE");
    } else {
        Log.e("TAG","HIDDEN");
    }

答案 1 :(得分:0)

试试这个

InputMethodManager imm = (InputMethodManager) getActivity()
            .getSystemService(Context.INPUT_METHOD_SERVICE);

    if (imm.isAcceptingText()) {
        writeToLog("Software Keyboard was shown");
    } else {
        writeToLog("Software Keyboard was not shown");
    }

答案 2 :(得分:0)

另一种解决方案:您可以找到键盘打开/关闭 如果您将windowSoftInputMode设置为adjustResize。如果将其设置为adjustPan,则仍无法使用其代码片段检测键盘是否可见。为了解决这个问题,我对上面的代码进行了微小的修改。

final View activityRootView = findViewById(R.id.activityRoot);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
    Rect r = new Rect();
    //r will be populated with the coordinates of your view that area still visible.
    activityRootView.getWindowVisibleDisplayFrame(r);

    int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
    if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
        ... do something here
    }
 }
}); 

答案 3 :(得分:0)

最后,我根据以下代码克服了我的问题

RelativeLayout contentView = (RelativeLayout)findViewById(R.id.activityRoot);
                    contentView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                        @Override
                        public void onGlobalLayout() {

                            Rect r = new Rect();
                            RelativeLayout contentView = (RelativeLayout)findViewById(R.id.activityRoot);
                            contentView.getWindowVisibleDisplayFrame(r);
                            int screenHeight = contentView.getRootView().getHeight();

                            // r.bottom is the position above soft keypad or device button.
                            // if keypad is shown, the r.bottom is smaller than that before.
                            int keypadHeight = screenHeight - r.bottom;

                            Log.d("Nifras", "keypadHeight = " + keypadHeight);

                            if (keypadHeight > screenHeight * 0.15) { // 0.15 ratio is perhaps enough to determine keypad height.
                                lytMaster.setTop(0);

                            }
                            else {
                                lytMaster.setBaselineAligned(true);
                                // keyboard is closed
                            }
                        }
                    });