前言

关于MAUI Blazor Android 输入框软键盘遮挡问题,之前的文章已经有了答案,MAUI Blazor Android 输入框软键盘遮挡问题

但是这个方案一直存在一点小的瑕疵

在小窗模式下,界面的高度始终不正确

所以本篇文章重点解决这个问题

特别感谢这篇文章 Android webView输入框软键盘遮挡问题-终极解决方案(不好使你打我) ,上一篇文章忘记感谢了,非常抱歉。

1. 小窗模式界面高度不正确

问题展示


可以很清楚的看到,小窗模式下,下面那一部分被吃掉了。这个是无法滚动的。

也就是说,界面渲染的高度实际上是大于小窗屏幕的高度。

那一部分哪去了,经过简单的调试,发现恰恰就是状态栏的高度

也就是代码中的这一部分

static int ComputeUsableHeight()
{
Rect rect = new Rect();
MChildOfContent?.GetWindowVisibleDisplayFrame(rect);
return rect.Bottom - rect.Top;
}

小窗模式时,rect.Top返回的永远是0,而不是状态栏高度

解决办法

我们重新写一个获取状态栏高度的方法,用它去替代rect.Top

static int ComputeUsableHeight()
{
Rect rect = new Rect();
MChildOfContent?.GetWindowVisibleDisplayFrame(rect);
return rect.Bottom - GetStatusBarHeight();
} static int GetStatusBarHeight()
{
int result = 0;
Resources resources = Activity.Resources ?? throw new InvalidOperationException("Activity Resources can't be null.");
int resourceId = resources.GetIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0)
{
result = resources.GetDimensionPixelSize(resourceId);
}
return result;
}

2. 全屏模式

然而又出现了新的问题,如果取消状态栏,那么界面高度又不正确了

很快找到解决办法

static int ComputeUsableHeight()
{
Rect rect = new Rect();
MChildOfContent.GetWindowVisibleDisplayFrame(rect);
if (IsImmersiveMode())
{
return rect.Bottom;
}
else
{
return rect.Bottom - GetStatusBarHeight();
}
} static bool IsImmersiveMode()
{
View decorView = Activity.Window.DecorView;
int uiOptions = (int)decorView.SystemUiVisibility;
return (uiOptions & (int)SystemUiFlags.Immersive) == (int)SystemUiFlags.Immersive;
}

4. 最终全部代码

Platform/Android/WebViewSoftInputPatch.cs

using Android.Content.Res;
using Android.Views;
using Android.Widget;
using System.Runtime.Versioning;
using static Android.Resource;
using Activity = Android.App.Activity;
using Rect = Android.Graphics.Rect;
using View = Android.Views.View; namespace MauiBlazor3.Platform.Android; [SupportedOSPlatform("Android")]
public static class WebViewSoftInputPatch
{
static Activity Activity => Microsoft.Maui.ApplicationModel.Platform.CurrentActivity ?? throw new InvalidOperationException("Android Activity can't be null.");
static View MChildOfContent;
static FrameLayout.LayoutParams FrameLayoutParams;
static int UsableHeightPrevious = 0; public static void Initialize()
{
FrameLayout content = (FrameLayout)Activity.FindViewById(Id.Content);
MChildOfContent = content.GetChildAt(0);
MChildOfContent.ViewTreeObserver.GlobalLayout += (s, o) => PossiblyResizeChildOfContent();
FrameLayoutParams = (FrameLayout.LayoutParams)MChildOfContent?.LayoutParameters;
} static void PossiblyResizeChildOfContent()
{
int usableHeightNow = ComputeUsableHeight();
if (usableHeightNow != UsableHeightPrevious)
{
int usableHeightSansKeyboard = MChildOfContent.RootView.Height;
int heightDifference = usableHeightSansKeyboard - usableHeightNow;
if (heightDifference < 0)
{
usableHeightSansKeyboard = MChildOfContent.RootView.Width;
heightDifference = usableHeightSansKeyboard - usableHeightNow;
} if (heightDifference > usableHeightSansKeyboard / 4)
{
FrameLayoutParams.Height = usableHeightSansKeyboard - heightDifference;
}
else
{
FrameLayoutParams.Height = usableHeightNow;
}
} MChildOfContent.RequestLayout();
UsableHeightPrevious = usableHeightNow;
} static int ComputeUsableHeight()
{
Rect rect = new Rect();
MChildOfContent.GetWindowVisibleDisplayFrame(rect);
if (IsImmersiveMode())
{
return rect.Bottom;
}
else
{
return rect.Bottom - GetStatusBarHeight();
}
} static int GetStatusBarHeight()
{
int result = 0;
Resources resources = Activity.Resources;
int resourceId = resources.GetIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0)
{
result = resources.GetDimensionPixelSize(resourceId);
}
return result;
} static bool IsImmersiveMode()
{
View decorView = Activity.Window.DecorView;
int uiOptions = (int)decorView.SystemUiVisibility;
return (uiOptions & (int)SystemUiFlags.Immersive) == (int)SystemUiFlags.Immersive;
}
}

Platform/Android/MainActivity.cs

protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
WebViewSoftInputPatch.Initialize();
}

MAUI Blazor Android 输入框软键盘遮挡问题2.0的更多相关文章

  1. Android软键盘遮挡的四种解决方案

    问题概述 在编辑框输入内容时会弹出软键盘,而手机屏幕区域有限往往会遮住输入界面,我们先看一下问题效果图: 输入用户名和密码时,系统会弹出键盘,造成系统键盘会挡住文本框的问题,如图所示: 输入密码时输入 ...

  2. Android软键盘遮挡布局的那些事

    有朋友提到软键盘遮挡布局的问题,说网上找了很多资料都未能解决,下面我来总结一下那些事,有些内容也是从网友那里学来的,但是我都会自己验证正确了才会贴出来. 首先来分析下我们常见的遮挡问题有哪些(本次所说 ...

  3. Android软键盘遮挡布局问题;

    布局被软键盘遮挡虽然不是什么大问题,但还是比较影响用户体验的:最让人恼火的是当前输入框被软键盘被遮挡,来看一下解决方法: 1.当前输入框被软键盘遮挡,仅把输入框显示出来,不改变整体布局: 设置Mani ...

  4. 移动端页面input输入框被键盘遮挡问题

    <body class="layout-fixed"> <!-- fixed定位的头部 --> <header> </header> ...

  5. Android隐藏软键盘收回软键盘

    代码改变世界 Android隐藏软键盘收回软键盘 InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPU ...

  6. ios上有时候提交按钮点击两次才可以取消输入框软键盘

    ios上有时候提交按钮点击两次才可以取消输入框软键盘,点击第一次软键盘消失,点击第二次输入框页面消失,这样用户体验不好.我的做法是用 touchstart 代替click来处理 反应快,但是有时候会出 ...

  7. Android WebView 软键盘挡住输入框

    解决方法一: 在所在的Activity中加入 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RES ...

  8. android开发 软键盘出现后 防止EditText控件遮挡 总体平移UI

    在EditText控件接近底部的情况下 软键盘弹出后会把获得焦点的EditText控件遮挡 无法看到输入信息  防止这种情况发生 就须要设置AndroidManifest.xml的属性 前面的xml信 ...

  9. 安卓H5软键盘遮挡输入框

    由于安卓app内嵌入H5页面,webview对于软键盘没有处理(如果不是产品强烈要求建议不要处理这种拆东墙补西墙的问题,因为其他的手机上可能会出现已经优化软键盘的情况) 1.input下方还有多余空位 ...

  10. ios移动输入框被软键盘遮挡

    页面输入框会出现被软键盘挡住的问题: 解决方法:获取input点击事件设置body高度 $('input').bind('click',function(e){ var $this = $(this) ...

随机推荐

  1. Karmada 多云容器编排引擎支持多调度组,助力成本优化

    摘要:Karmada 社区也在持续关注云成本的管理,在最近发布的 v1.5 版本中,支持用户在分发策略 PropagationPolicy/ClusterPropagationPolicy 中设置多个 ...

  2. vue2项目中调取登录接口登录以后获取个人信息以后,储存在哪里,怎么在不同的页面展示想要的信息?

    在Vue2项目中,可以将个人信息存储在Vuex状态管理中或者浏览器的本地存储中,具体取决于项目的需求和规模. 1. Vuex状态管理 在Vuex中定义一个user模块,用于存储用户信息,可以在登录成功 ...

  3. VueHub:我用 ChatGPT 开发的第一个项目,送给所有 Vue 爱好者

    大家好,我是DOM哥. 我用 ChatGPT 开发了一个 Vue 的资源导航网站. 不管你是资深 Vue 用户,还是刚入门想学习 Vue 的小白,这个网站都能帮助到你. 网站地址:https://do ...

  4. Python 项目:外星人入侵--第三部分

    1.项目内容: 在屏幕左上角添加一个外星人,并指定合适的边框,根据第一个外星人的边距和屏幕尺寸计算屏幕上可容纳多少个外星人. 让外星人群向两边和下方移动,直到外星人被全部击落,有外星人撞到飞船,或有外 ...

  5. 2020-09-14:KVM和XEN虚拟化的区别?

    福哥答案2020-09-14:#福大大架构师每日一题#[答案来自此链接](https://bbs.csdn.net/topics/397671000)KVM:1.虚拟化支持:全虚拟化.2.支持架构:虚 ...

  6. 基于Gazebo搭建移动机器人,并结合SLAM系统完成定位和建图仿真

    博客地址:https://www.cnblogs.com/zylyehuo/ gazebo小车模型创建及仿真详见之前博客 gazebo小车模型(附带仿真环境) - zylyehuo - 博客园 gaz ...

  7. Django4全栈进阶之路18 项目实战(用户管理):user_edit.html用户编辑画面设计

    1.模块 {% extends 'base.html' %} {% block content %} <!-- 编辑用户表单 --> <div class="card mt ...

  8. pages.json 文件:自定义导航栏

    自定义导航栏使用注意 当navigationStyle设为custom或titleNView设为false时,原生导航栏不显示,此时要注意几个问题: 非H5端,手机顶部状态栏区域会被页面内容覆盖.这是 ...

  9. Redis 高级特性 Redis Stream使用

    Redis Stream 简介 Redis Stream 是 Redis 5.0 版本新增加的数据结构. Stream从字面上看是流类型,但其实从功能上看,应该是Redis对消息队列(MQ,Messa ...

  10. 【Java】JTable的数据刷新

    前言 这段时间在写一个大实验,水果超市管理系统,yes,我觉得挺大的,但是就当成了一个实验,接下来还有一个课程设计和一个实训,more bigger... 问题 在我把其他的都写好的时候去写UI层,发 ...