gpt4 book ai didi

android - 始终隐藏 Xamarin Forms Android Entry Control 的键盘(条码扫描器输入)

转载 作者:行者123 更新时间:2023-11-29 16:46:25 25 4
gpt4 key购买 nike

我是 Xamarin.Forms 和移动应用开发的新手,非常感谢您的耐心和帮助!正在使用 Xamarin.Forms PCL 构建条形码扫描器应用程序,尝试使用 MVVM。扫描仪是外部蓝牙设备(因此不能使用 ZXing)。

此项目有一个固定要求,即使用扫描仪作为键盘输入,并让用户能够快速将一个蓝牙设备换成另一个品牌(因此不能使用特定于设备的 API)。第二个要求是永远不允许用户直接在 Entry 控件中键入任何内容。输入应该来自扫描仪,而且只能来自扫描仪,因此我们不希望键盘显示在扫描页面上。

还有其他页面具有输入控件,用户需要访问键盘,即使显示非扫描屏幕,扫描仪也应该能够与蓝牙保持连接。因此,我需要一种可靠的方法来将软键盘设置为从不在扫描页面上显示(此页面上只有一个输入控件,并且仅供扫描仪使用),但允许在其他设备上访问键盘页面。

当在扫描页面上时,我们希望始终将焦点设置在扫描仪的 Entry 控件上,因此当控件收到 Completed 事件时,我们用收到的值做一些事情,然后清除控件并重新设置焦点为下一次扫描做准备。

我一直在编写自定义控件和 android 渲染器,以及设置依赖项(首选),都取得了部分成功。无论哪种方式,都存在与将焦点设置在控件上的时间相关的时间问题。如果在设置焦点之前没有足够的延迟,软键盘将保持可见。在提供的代码示例中,我添加了一个短暂的 sleep 延迟,主要用于隐藏键盘。但是,每次扫描时键盘仍然会在屏幕上短暂“闪烁”,这看起来很糟糕。真的更喜欢一个不那么笨拙和丑陋的解决方案。

有没有一种好的、简单的方法可以完全移除页面的软键盘,同时仍然允许输入控件接收焦点,以便可以接收扫描的条形码?和/或任何其他能让我仍然满足要求的建议?

(PS:扫描页面目前没有使用MVVM绑定(bind)。只是想先让键盘消失,然后再进行绑定(bind)。)

下面是我尝试解决的一种方法。还有其他人。注意:最终我采用了一种完全不同的方法,我将其作为答案发布。

自定义控件(在 PCL 中):

using Xamarin.Forms;

namespace MyPCL.Views
{
//See ScanEntryRenderer in the Android project.
public class ScanEntryControl : Entry
{
public ScanEntryControl() { }
}
}

Xaml 页面(注意自定义控件上的 InputTransparent = "True"。这样用户就不能直接在 android 设备上输入。所有输入都必须来自蓝牙扫描仪)。

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MyPCL.Views"
x:Class="MyPCL.Views.ScanTestPage"
Title="Scan Test Page" >
<ContentPage.Content>
<StackLayout>
<Label Text="Scanner Test" />
<local:ScanEntryControl x:Name="BarcodeEntry"
Completed="BarcodeEntryCompleted"
InputTransparent="True"/>
<Label x:Name="ResultLabel" />
</StackLayout>
</ContentPage.Content>
</ContentPage>

表单背后的代码:

using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace MyPCL.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ScanTestPage : ContentPage
{
public ScanTestPage()
{
InitializeComponent();
BarcodeEntry.Focus();
}

protected override void OnAppearing()
{
base.OnAppearing();
BarcodeEntry.Focus();
}

private void BarcodeEntryCompleted(object sender, EventArgs e)
{
if (!string.IsNullOrWhiteSpace(BarcodeEntry.Text))
{
ResultLabel.Text = "You entered: " + BarcodeEntry.Text;
BarcodeEntry.Text = string.Empty;
}
BarcodeEntry.Focus();
}
}
}

Android 渲染器:

using Android.Content;
using Xamarin.Forms;
using MyPCL.Views;
using MyPCL.Droid;
using Xamarin.Forms.Platform.Android;
using Android.Views.InputMethods;

[assembly: ExportRenderer(typeof(ScanEntryControl), typeof(ScanEntryRenderer))]
namespace MyPCL.Droid
{
public class ScanEntryRenderer : EntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
((ScanEntryControl)e.NewElement).PropertyChanging += OnPropertyChanging;
}

if (e.OldElement != null)
{
((ScanEntryControl)e.OldElement).PropertyChanging -= OnPropertyChanging;
}

// Disable the Keyboard on Focus
this.Control.ShowSoftInputOnFocus = false;
}

private void OnPropertyChanging(object sender, PropertyChangingEventArgs propertyChangingEventArgs)
{
// Check if the view is about to get Focus
if (propertyChangingEventArgs.PropertyName == VisualElement.IsFocusedProperty.PropertyName)
{
// Dismiss the Keyboard
InputMethodManager imm = (InputMethodManager)this.Context.GetSystemService(Context.InputMethodService);
imm.HideSoftInputFromWindow(this.Control.WindowToken, 0);
}
}
}
}

最佳答案

I have been stumbling around writing custom controls and android renderers, and with setting up dependencies (preferred), both with partial success.

你可以在你的扫描页面中使用EditText.ShowSoftInputOnFocus来实现,这样当你的输入获得焦点时键盘就不会出现了:

using Android.Content;
using Android.Views.InputMethods;
using Edi;
using Edi.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(ScanEntryControl), typeof(ScanEntryRenderer))]
namespace Edi.Droid
{
public class ScanEntryRenderer : EntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
((ScanEntryControl)e.NewElement).PropertyChanging += OnPropertyChanging;
}

if (e.OldElement != null)
{
((ScanEntryControl)e.OldElement).PropertyChanging -= OnPropertyChanging;
}

// Disable the Keyboard on Focus
this.Control.ShowSoftInputOnFocus = false;
}

private void OnPropertyChanging(object sender, PropertyChangingEventArgs propertyChangingEventArgs)
{
// Check if the view is about to get Focus
if (propertyChangingEventArgs.PropertyName == VisualElement.IsFocusedProperty.PropertyName)
{
// incase if the focus was moved from another Entry
// Forcefully dismiss the Keyboard
InputMethodManager imm = (InputMethodManager)this.Context.GetSystemService(Context.InputMethodService);
imm.HideSoftInputFromWindow(this.Control.WindowToken, 0);
}
}
}
}

在其他页面你仍然可以使用Entry,所以键盘会出现。

更新:

PCL 中的

ScanEntryControl 类:

using Xamarin.Forms;

namespace Edi
{
public class ScanEntryControl : Entry
{
}
}

.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"
xmlns:local="clr-namespace:Edi"
x:Class="Edi.MainPage">
<ContentPage.Content>
<StackLayout>
<local:ScanEntryControl Text="ScanEntryControl"/>
<Entry Text="Entry"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>

关于android - 始终隐藏 Xamarin Forms Android Entry Control 的键盘(条码扫描器输入),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47878354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com