Home > Java > javaTutorial > body text

Implementation code for monitoring soft keyboard display status in Android

高洛峰
Release: 2017-01-07 11:58:14
Original
916 people have browsed it

/**监听软键盘状态
   * @param activity
   * @param listener
   */
  public static void addOnSoftKeyBoardVisibleListener(Activity activity, final OnSoftKeyBoardVisibleListener listener) {
    final View decorView = activity.getWindow().getDecorView();
    decorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
      @Override
      public void onGlobalLayout() {
        Rect rect = new Rect();
        decorView.getWindowVisibleDisplayFrame(rect);
        int displayHight = rect.bottom - rect.top;
        int hight = decorView.getHeight();
        boolean visible = (double) displayHight / hight < 0.8;
          
        Log.d(TAG, "DecorView display hight = " + displayHight);
        Log.d(TAG, "DecorView hight = " + hight);
        Log.d(TAG, "softkeyboard visible = " + visible);
  
        if(visible != sLastVisiable){
          listener.onSoftKeyBoardVisible(visible);
        }
        sLastVisiable = visible;
      }
    });
  }
Copy after login


For more articles related to the code for monitoring the soft keyboard display status in Android, please pay attention to the PHP Chinese website!


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!