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

武汉市东西湖城乡建设局网站对网络推广的理解

武汉市东西湖城乡建设局网站,对网络推广的理解,重庆网站建设制作公司,购物网站商城策划思路: 要在 Android 应用中全局检测软键盘的弹起,您可以使用 ViewTreeObserver.OnGlobalLayoutListener 监听器来监听布局树的变化。当软键盘弹起或隐藏时,布局树会发生变化,因此您可以在监听器中捕获这些变化。 以下是一个示例…

思路:

要在 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.hengruixuexiao.com/news/44651.html

相关文章:

  • es网站建设百度收录的网站
  • 找生产厂家的网站百度做网站推广电话
  • 给个网站带颜色广告联盟接广告
  • 企业公司网站制作黑科技推广软件
  • 高密哪里有做网站的白百度一下你就知道
  • 网站建设管理工作经验介绍百度安装
  • 如何做网站拉动条北京网站优化企业
  • 免费做网站支持绑定品牌网络营销策划书
  • 做网站初级教程百度站长平台注册
  • 网站建设与维护管理办法竞价推广员月挣多少
  • 网站建设毕业设计开题报告百度seo分析工具
  • 咸阳学校网站建设公司打开百度搜索引擎
  • 日本可以做的h游戏视频网站seo标题优化的心得总结
  • 沈阳网站建设方案网站查询入口
  • 网站查询服务器ip欧洲网站服务器
  • 网站制作软件图标武汉seo关键字推广
  • 常州外贸集团 网站建设海口关键词优化报价
  • 初中信息技术 网站制作如何利用互联网宣传与推广
  • 大型建设工程类考试辅导网站爱站seo工具
  • 做与不做赞美网站百度小程序怎么进入
  • 网站怎么做图片转换软文推广做得比较好的推广平台
  • 怎么用手机做网站平台网站注册账号
  • 外国人做的汉字网站今天nba新闻最新消息
  • 地下城做心悦任务的网站在哪里可以免费自学seo课程
  • 网站设计报价方案产品推广活动策划方案
  • 弹性云主机做网站如何在百度上推广业务
  • 江津哪里找做网站的免费单页网站在线制作
  • 网站开发工作周记网页制作代码大全
  • 良匠网站建设系统优化工具
  • 纯静态企业网站软文街怎么样