How do I detect if software keyboard is visible on Android Device or not?

前端 未结 30 1357
情书的邮戳
情书的邮戳 2020-11-22 10:59

Is there a way in Android to detect if the software (a.k.a. \"soft\") keyboard is visible on screen?

相关标签:
30条回答
  • 2020-11-22 11:52

    a may be using :

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    
        Log.d(
        getClass().getSimpleName(), 
        String.format("conf: %s", newConfig));
    
        if (newConfig.hardKeyboardHidden != hardKeyboardHidden) {
            onHardwareKeyboardChange(newConfig.hardKeyboardHidden);
    
            hardKeyboardHidden = newConfig.hardKeyboardHidden;
        }
    
        if (newConfig.keyboardHidden != keyboardHidden) {
            onKeyboardChange(newConfig.keyboardHidden);
    
            keyboardHidden = newConfig.hardKeyboardHidden;
        }
    
    }
    
    public static final int KEYBOARDHIDDEN_UNDEFINED = 0;
    public static final int KEYBOARDHIDDEN_NO = 1;
    public static final int KEYBOARDHIDDEN_YES = 2;
    public static final int KEYBOARDHIDDEN_SOFT = 3;
    
    //todo
    private void onKeyboardChange(int keyboardHidden) {
    
    }
    
    //todo
    private void onHardwareKeyboardChange(int hardKeyboardHidden) {
    
    }
    
    0 讨论(0)
  • 2020-11-22 11:53

    In Android you can detect through ADB shell. I wrote and use this method:

    {
            JSch jsch = new JSch();
            try {
                Session session = jsch.getSession("<userName>", "<IP>", 22);
                session.setPassword("<Password>");
                Properties config = new Properties();
                config.put("StrictHostKeyChecking", "no");
                session.setConfig(config);
                session.connect();
    
                ChannelExec channel = (ChannelExec)session.openChannel("exec");
                BufferedReader in = new BufferedReader(new    
                InputStreamReader(channel.getInputStream()));
                channel.setCommand("C:/Android/android-sdk/platform-tools/adb shell dumpsys window 
                InputMethod | findstr \"mHasSurface\"");
                channel.connect();
    
                String msg = null;
                String msg2 = " mHasSurface=true";
    
                while ((msg = in.readLine()) != null) {
                    Boolean isContain = msg.contains(msg2);
                    log.info(isContain);
                    if (isContain){
                        log.info("Hiding keyboard...");
                        driver.hideKeyboard();
                    }
                    else {
                        log.info("No need to hide keyboard.");
                    }
                }
    
                channel.disconnect();
                session.disconnect();
    
            } catch (JSchException | IOException | InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 11:53

    As you might know android Software keyboard will be visible only when there is a possible event of typing. In other words Keyboard get visible only when EditText is focused. that means you can get weather the Keyboard is visible or not by using OnFocusChangeListener.

    //Declare this Globally
    
    public boolean isKeyBoardVisible = false;
    
    //In OnCreate *[For Activity]*, OnCreateView *[For Fragment]*
    
    text_send.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus)
                isKeyBoardVisible = true;
            else
                isKeyBoardVisible = false;
        }
    });
    

    Now you can use isKeyBoardVisible variable anywhere in the class to get weather the keyboard is Open or Not. It worked well for me.

    Note: This process doesn't work when the Keyboard is opened programmatically using InputMethodManager because that doesn't invoke OnFocusChangeListener.

    0 讨论(0)
  • 2020-11-22 11:54

    There is no direct way - see http://groups.google.com/group/android-platform/browse_thread/thread/1728f26f2334c060/5e4910f0d9eb898a where Dianne Hackborn from the Android team has replied. However, you can detect it indirectly by checking if the window size changed in #onMeasure. See How to check visibility of software keyboard in Android?.

    0 讨论(0)
  • 2020-11-22 11:54

    Very Easy

    1. Put id on your root view

    rootView is just a view pointing to my root view in this case a relative layout:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/addresses_confirm_root_view"
                    android:background="@color/WHITE_CLR">
    

    2. Initialize your root view in your Activity:

    RelativeLayout rootView = (RelativeLayout) findViewById(R.id.addresses_confirm_root_view);

    3. Detect if keyboard is opened or closed by using getViewTreeObserver()

        rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    int heightDiff = rootView.getRootView().getHeight() - rootView.getHeight();
    
                    if (heightDiff > 100) { 
                        Log.e("MyActivity", "keyboard opened");
                    } else { 
                        Log.e("MyActivity", "keyboard closed");
                    }
                }
            });
    
    0 讨论(0)
  • 2020-11-22 11:55

    I converted the answer to the kotlin, hope this helps for kotlin users.

    private fun checkKeyboardVisibility() {
        var isKeyboardShowing = false
    
        binding.coordinator.viewTreeObserver.addOnGlobalLayoutListener {
            val r = Rect()
            binding.coordinator.getWindowVisibleDisplayFrame(r)
            val screenHeight = binding.coordinator.rootView.height
    
            // r.bottom is the position above soft keypad or device button.
            // if keypad is shown, the r.bottom is smaller than that before.
            val keypadHeight = screenHeight - r.bottom
    
    
            if (keypadHeight > screenHeight * 0.15) { // 0.15 ratio is perhaps enough to determine keypad height.
                // keyboard is opened
                if (!isKeyboardShowing) {
                    isKeyboardShowing = true
    
                }
            } else {
                // keyboard is closed
                if (isKeyboardShowing) {
                    isKeyboardShowing = false
    
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题