广告
返回顶部
首页 > 资讯 > 精选 >Android项目中怎么显示与隐藏软键盘
  • 183
分享到

Android项目中怎么显示与隐藏软键盘

androidroi目中 2023-05-31 13:05:22 183人浏览 泡泡鱼
摘要

今天就跟大家聊聊有关Android项目中怎么显示与隐藏软键盘,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。代码如下:import android.app.Activity;impor

今天就跟大家聊聊有关Android项目中怎么显示与隐藏软键盘,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

代码如下:

import android.app.Activity;import android.content.Context;import android.graphics.Rect;import android.os.Build;import android.util.Log;import android.util.TypedValue;import android.view.View;import android.view.ViewGroup;import android.view.ViewTreeObserver;public class KeyBoardShowListener {  private Context ctx;  public KeyBoardShowListener(Context ctx) {    this.ctx = ctx;  }  OnKeyboardVisibilityListener keyboardListener;  public OnKeyboardVisibilityListener geTKEyboardListener() {    return keyboardListener;  }  public interface OnKeyboardVisibilityListener {    void onVisibilityChanged(boolean visible);  }  public void setKeyboardListener(final OnKeyboardVisibilityListener listener, Activity activity) {    final View activityRootView = ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);    activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {      private boolean wasOpened;      private final int DefaultKeyboardDP = 100;      // From @nathanielwolf answer... Lollipop includes button bar in the root. Add height of button bar (48dp) to maxDiff      private final int EstimatedKeyboardDP = DefaultKeyboardDP + (Build.VERSioN.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? 48 : 0);      private final Rect r = new Rect();      @Override      public void onGlobalLayout() {        // Convert the dp to pixels.        int estimatedKeyboardHeight = (int) TypedValue            .applyDimension(TypedValue.COMPLEX_UNIT_DIP, EstimatedKeyboardDP, activityRootView.getResources().getDisplayMetrics());        // Conclude whether the keyboard is shown or not.        activityRootView.getWindowVisibleDisplayFrame(r);        int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);        boolean isshown = heightDiff >= estimatedKeyboardHeight;        if (isShown == wasOpened) {          Log.e("Keyboard state", "Ignoring global layout change...");          return;        }        wasOpened = isShown;        listener.onVisibilityChanged(isShown);      }    });  }}

--结束END--

本文标题: Android项目中怎么显示与隐藏软键盘

本文链接: https://www.lsjlt.com/news/225722.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作