当前位置: 首页 > news >正文

Android 全局监听软键盘弹起隐藏 动态修改布局并适配无限循环的问题

思路:

要在 Android 应用中全局检测软键盘的弹起,您可以使用 ViewTreeObserver.OnGlobalLayoutListener 监听器来监听布局树的变化。当软键盘弹起或隐藏时,布局树会发生变化,因此您可以在监听器中捕获这些变化。

以下是一个示例,展示如何在全局范围内检测软键盘的弹起:

import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import androidx.appcompat.app.AppCompatActivity;public class MainActivity extends AppCompatActivity {private View rootView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);rootView = findViewById(R.id.rootView); // Replace with your root layout's ID// Register a global layout listenerrootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {@Overridepublic void onGlobalLayout() {// Get the height of the visible display areaRect rect = new Rect();rootView.getWindowVisibleDisplayFrame(rect);int screenHeight = rootView.getHeight();int keyboardHeight = screenHeight - rect.bottom;// If the keyboard height is greater than a threshold (e.g., 100 pixels), the keyboard is likely visibleif (keyboardHeight > 100) {// Keyboard is visible, do something} else {// Keyboard is hidden, do something else}}});}
}

在上面的代码中,rootView 是您布局的根视图,您需要将其替换为您实际布局的根视图。addOnGlobalLayoutListener 方法用于注册一个监听器,当布局树发生变化时会调用 onGlobalLayout 方法。

onGlobalLayout 方法中,您可以通过比较屏幕高度和可见区域的底部位置来计算软键盘的高度。根据计算结果,您可以判断软键盘是否可见,并执行相应的操作。

以下是一个在项目中的实际示例,解决无限循环的示例:

private void changerBottomView() {binding.includedNoteMenu.fragmentLl.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);
}private ViewTreeObserver.OnGlobalLayoutListener onGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {@Overridepublic void onGlobalLayout() {Rect rect = new Rect();//取得 rootView 可视区域binding.includedNoteMenu.fragmentLl.getWindowVisibleDisplayFrame(rect);//取得 rootView 不可视区域高度 (被其他View遮挡的区域高度)int rootInvisibleHeight = binding.includedNoteMenu.fragmentLl.getRootView().getHeight() - rect.bottom;LogUtil.i("shawn","rootInvisibleHeight = " + rootInvisibleHeight);ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams)binding.includedNoteMenu.fragmentLl.getLayoutParams();if (layoutParams.bottomMargin != rootInvisibleHeight + UiUtil.dp2px(10)) {layoutParams.bottomMargin = rootInvisibleHeight + UiUtil.dp2px(10);binding.includedNoteMenu.fragmentLl.setLayoutParams(layoutParams);}}};

日志: 只会多调用一次,在第二次回调时,条件已不满足,不会再调用setLayoutParams

I/shawn: [ main: AddNoteFragment.java:173 onGlobalLayout ] - rootInvisibleHeight = 840
I/shawn: [ main: AddNoteFragment.java:173 onGlobalLayout ] - rootInvisibleHeight = 840
I/shawn: [ main: AddNoteFragment.java:173 onGlobalLayout ] - rootInvisibleHeight = -4
I/shawn: [ main: AddNoteFragment.java:173 onGlobalLayout ] - rootInvisibleHeight = -4

http://www.lryc.cn/news/140299.html

相关文章:

  • 第 k 小整数
  • LeetCode 1448. 统计二叉树中好节点的数目:DFS
  • AR室内导航技术之技术说明与效果展示
  • 06-Numpy基础-线性代数
  • SpringBootWeb 登录认证
  • 【JVM 内存结构丨栈】
  • LeetCode 138.复制带随机指针的链表
  • 基于SSM的小说网站的设计与实现(论文+源码)_kaic
  • 【Python】代理池针对ip拦截破解
  • P1065 [NOIP2006 提高组] 作业调度方案
  • 设计模式三原则
  • dll载入时发生的事情
  • k8s-ingress-context deadline exceeded
  • css盒模型
  • cuda11.1和cuDNN v8.8.1的安装目录问题
  • 微信小程序scroll-view的触发机制
  • 为本地文件创建URL
  • UI位置与布局
  • 《存储IO路径》专题:DDIO对系统性能的影响
  • ModaHub魔搭社区:WinPlan经营大脑数据采集
  • 缓存最佳实践
  • Linux 终端命令之文件目录操作,对比Dos相关命令
  • C++学习第十八天----switch语句
  • 基于poi生成excel模板并生成下拉选择框
  • Redis五种类型
  • 通过IP地址如何防范钓鱼网站诈骗?
  • useEffect使用详解
  • element-table的动态操作,自动以表格,动态新增行、列,删除行列
  • python--文件管理系统
  • uniapp 微信小程序:RecorderManager 录音DEMO