Xamarin Android:自定义渲染器的自定义键盘存在问题-出现在控件下方,按键无法按下等

如何解决Xamarin Android:自定义渲染器的自定义键盘存在问题-出现在控件下方,按键无法按下等

我正在创建具有自定义键盘的自定义控件。我遇到了几个问题。

我正在使用Visual Studio Community 2019,并且正在使用Android_Accelerated_x86_Oreo(Android 8.1 API-27)作为我的Android模拟器。

总之,

我需要对Entry进行不同的样式设置,因此我从中创建一个派生类,称为TextBoxEntry。

我在MainPage.cs中以编程方式创建了所有控件,因此MainPage.xaml为空白(差不多)。

Android专用代码(即渲染器)被塞入MainActivity.cs。

最后,自定义键盘在Resources / xml / keyboard.xml下定义。

我无法解决这些问题(已解决5个问题中的3个):

  1. 为什么自定义渲染器的自定义键盘会出现在控件下方?

  2. 出现键盘时,我需要获取其高度,以便可以相应地修改scrollview尺寸。不幸的是,在ControlOnFocusChanged()[EditText.FocusChange事件处理程序]中执行此操作不起作用,因为在该时间点上,键盘仍未显示,因此其高度为0。或者我应该在其中进行操作其他事件吗? 应改用ViewTreeObserver.IOnGlobalLayoutListener。

  3. 键盘上的键不可按下。有时,键盘会隐藏起来。我想我按这些键时,实际输入会失去焦点。无论如何,我实际上在哪里出错?

  4. scrollview的最后一列以及最后一行的TextBoxEntries被剪掉。再说一次,我不知道出了什么问题。 通过将“ margin”设置为1可以解决此问题。

  5. 在ControlOnFocusChange()内,a_height提供1280,而b_height提供342。scrollview占据整个屏幕的一半以上,但是其高度远非整个屏幕的一半。这两个以不同的单位显示吗? 根据Find our the default height of the controls in Xamarin Forms,似乎两个高地都使用不同的单位...好吧,可以通过一些转换来解决。

因此,这是所有相关文件的内容:

MainPage.xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="CustomKeyboard.MainPage"/>

MainPage.xaml.cs:

using Xamarin.Forms;

namespace CustomKeyboard
{
    public class TextBoxEntry : Entry
    {
        public ScrollView ScrollView { get; set; } = null; // So that I could easily get its reference and work on it...
    }
    public partial class MainPage : ContentPage
    {
        AbsoluteLayout layout = null;
        ScrollView scrollview = null;
        Grid grid = null;

        public MainPage()
        {
            InitializeComponent();
            BuildControls();
            BuildTextBoxes();
        }
        private void BuildControls()
        {
            layout = new AbsoluteLayout();
            scrollview = new ScrollView();
            grid = new Grid();
            layout.Children.Add(scrollview,new Rectangle(0.5d,0d,0.95d,0.85d),AbsoluteLayoutFlags.All);
            scrollview.BackgroundColor = Color.Yellow;
            scrollview.Padding = 5;
            scrollview.Content = grid;
            Content = layout;
        }
        private void BuildTextBoxes()
        {
            for (var i = 0; i < 50; ++i)
            {
                if (i % 5 == 0)
                {
                    var l = new Label()
                    {
                        Text = (i / 5).ToString(),HorizontalTextAlignment = TextAlignment.Center,VerticalTextAlignment = TextAlignment.Center,};
                    grid.Children.Add(l,i / 5);
                }
                else
                {
                    var r = i / 5;
                    var c = i % 5;
                    var e = new TextBoxEntry();
                    e.Margin = 1; // THIS SOLVES ISSUE 4
                    e.ScrollView = scrollview;
                    grid.Children.Add(e,c,r);
                }
            }
        }
    }
}

MainActivity.cs:

using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.InputMethodServices;
using Android.OS;
using Android.Support.Design.Widget;
using Android.Views;
using Android.Widget;

using Java.Lang;

using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Xamarin.Essentials;

using CustomKeyboard;
using CustomKeyboard.Droid;

[assembly: ExportRenderer(typeof(TextBoxEntry),typeof(TextBoxEntryRenderer))]

namespace CustomKeyboard.Droid
{
    [Activity(
        Label = "CustomKeyboard",Icon = "@mipmap/icon",Theme = "@style/MainTheme",MainLauncher = true,ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation
    )]

    public class MainActivity : FormsAppCompatActivity
    {
        internal static MainActivity Instance { get; private set; }
        protected override void OnCreate(Bundle savedInstanceState)
        {
            Instance = this;

            base.OnCreate(savedInstanceState);

            Xamarin.Essentials.Platform.Init(this,savedInstanceState);
            Forms.Init(this,savedInstanceState);
            LoadApplication(new App());
        }
    }

    public class TextBoxEntryRenderer : ViewRenderer<TextBoxEntry,TextInputLayout>
    {
        public KeyboardView mKeyboardView;
        ViewGroup activityRootView;
        protected EditText editText => Control.EditText;

        public TextBoxEntryRenderer(Context context) : base(context) { }

        protected override TextInputLayout CreateNativeControl()
        {
            #region Add keyboard
            var activity = MainActivity.Instance as Activity;
            activity.Window.SetSoftInputMode(SoftInput.StateAlwaysHidden);

            var rootView = activity.Window.DecorView.FindViewById(Android.Resource.Id.Content);
            activityRootView = ((ViewGroup)rootView).GetChildAt(0) as ViewGroup;

            mKeyboardView = new KeyboardView(MainActivity.Instance,null);
            mKeyboardView.PreviewEnabled = false; //Removes magnified key popups on key press
            mKeyboardView.Visibility = ViewStates.Gone;
            mKeyboardView.Keyboard = new Android.InputMethodServices.Keyboard(Context,Resource.Xml.keyboard);
            mKeyboardView.Key += (sender,e) =>
            {
                long eventTime = JavaSystem.CurrentTimeMillis();
                DispatchKeyEvent(new KeyEvent(eventTime,eventTime,KeyEventActions.Down,e.PrimaryCode,KeyEventFlags.SoftKeyboard | KeyEventFlags.KeepTouchMode));

                Task.Delay(1);
            };
            Android.Widget.RelativeLayout.LayoutParams layoutParams =
                new Android.Widget.RelativeLayout.LayoutParams(LayoutParams.MatchParent,LayoutParams.WrapContent); // or wrap_content
            layoutParams.AddRule(LayoutRules.AlignParentBottom);
            activityRootView.AddView(mKeyboardView,layoutParams);
            #endregion

            var _editText = new EditText(Context);
            _editText.SetTextColor(Android.Graphics.Color.Black);
            var textInputLayout = new TextInputLayout(Context);
            textInputLayout.AddView(_editText);
            return textInputLayout;
        }
        protected override void OnElementChanged(ElementChangedEventArgs<TextBoxEntry> e)
        {
            base.OnElementChanged(e);
            if (e.OldElement != null && Control != null) editText.FocusChange -= ControlOnFocusChange;
            if (e.NewElement != null)
            {
                SetNativeControl(CreateNativeControl());
                editText.ShowSoftInputOnFocus = false;
                editText.FocusChange += ControlOnFocusChange;
                editText.SetPadding(20,20,0);
                editText.SetTextSize(Android.Util.ComplexUnitType.Px,32);
                editText.TextChanged += (source,args) => { };
            }
            if (Control != null)
            {
                GradientDrawable gd = new GradientDrawable();
                gd.SetColor(Android.Graphics.Color.Transparent);
                Control.SetBackground(gd);
                editText.Background = null; // Remove underline.
                var shape = new ShapeDrawable(new Android.Graphics.Drawables.Shapes.RectShape());
                shape.Paint.Color = Xamarin.Forms.Color.Gray.ToAndroid();
                shape.Paint.SetStyle(Paint.Style.Stroke);
                Control.Background = shape;
            }
        }
        private void ControlOnFocusChange(object sender,FocusChangeEventArgs args)
        {
            if (args.HasFocus)
            {
                editText.Post(() =>
                {
                    editText.RequestFocus();
                    if (mKeyboardView.Visibility == ViewStates.Gone) mKeyboardView.Visibility = ViewStates.Visible;

                    // Wanna resize scrollview,but couldn't get necessary info...!
                    var deviceDisplayInfo = DeviceDisplay.MainDisplayInfo;
                    var a_height = deviceDisplayInfo.Height;
                    var b_height = Element.ScrollView.Height;
                    var c_height = mKeyboardView.Height;
                });
            }
            else
            {
                // Hide keyboard
                mKeyboardView.Visibility = ViewStates.Gone;
            }
        }
    }
}

最后, 资源/xml/keyboard.xml:

<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:keyWidth="25%p" android:horizontalGap="0px"
    android:verticalGap="0px" android:keyHeight="27dip">

  <Row>
    <Key android:codes="8"  android:keyLabel="1" android:keyHeight="54dip" android:keyEdgeFlags="left" />
    <Key android:codes="9"  android:keyLabel="2" android:keyHeight="54dip" />
    <Key android:codes="10" android:keyLabel="3" android:keyHeight="54dip" />
    <Key android:codes="81" android:keyLabel="+" android:keyHeight="81dip" android:keyEdgeFlags="right" />
  </Row>
  <Row></Row>
  <Row>
    <Key android:codes="11" android:keyLabel="4" android:keyHeight="54dip" android:keyEdgeFlags="left" />
    <Key android:codes="12" android:keyLabel="5" android:keyHeight="54dip" />
    <Key android:codes="13" android:keyLabel="6" android:keyHeight="54dip" />
  </Row>
  <Row>
    <Key android:codes="66" android:keyLabel="RET" android:keyHeight="81dip" android:horizontalGap="75%p" android:keyEdgeFlags="right" />
  </Row>
  <Row>
    <Key android:codes="14" android:keyLabel="7" android:keyHeight="54dip" android:keyEdgeFlags="left" />
    <Key android:codes="15" android:keyLabel="8" android:keyHeight="54dip" />
    <Key android:codes="16" android:keyLabel="9" android:keyHeight="54dip" />
  </Row>
  <Row></Row>
  <Row>
    <Key android:codes="17" android:keyLabel="*" android:keyHeight="54dip" android:keyEdgeFlags="left" />
    <Key android:codes="7"  android:keyLabel="0" android:keyHeight="54dip" />
    <Key android:codes="18" android:keyLabel="#" android:keyHeight="54dip" />
    <Key android:codes="67" android:keyLabel="DEL" android:keyHeight="54dip" android:keyEdgeFlags="right" />
  </Row>
  <Row></Row>

</Keyboard>

解决方法

为什么自定义渲染器的自定义键盘出现在控件下方?

alignParentBottom属性设置为true,以使我们的键盘从屏幕的底部可见。

android:layout_alignParentBottom="true"

键盘上的键不可按下。有时,键盘会隐藏起来。我想我按这些键时,实际输入会失去焦点。无论如何,我实际上在哪里出错?

为我们的键可以具有的两个状态定义一个状态选择器:正常(不按)和按true或false按下。

enter image description here

您可以从GitHub下载源文件。 https://github.com/WendyZang/CustomKeyboardXamarinForms

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res