取消软键盘
本文关键字:键盘 取消 | 更新日期: 2023-09-27 18:01:59
我有一个页面,其中的想法是使用外部键盘。当页面加载时,我将焦点设置在Entry
控件上,并且我想隐藏软键盘。
这就是我要做的类:
internal class RedactContent : ContentPage
{
StackLayout stack = new StackLayout();
Entry entry;
internal RedactContent()
{
entry = new Entry();
Content = new StackLayout
{
Children = {
entry,
//more code
}
};
}
protected override void OnAppearing()
{
base.OnAppearing();
entry.Focus();
// Hide Keyboard
}
}
我该怎么做呢?
可以用CloseKeyboard方法创建CustomEntry。为此,您需要为每个平台编写自定义渲染器。见http://developer.xamarin.com/guides/cross-platform/xamarin-forms/custom-renderer/
在Android上,你的CustomEntry类看起来像这样:
[assembly: ExportRenderer(typeof(CustomEntry), typeof(CustomEntryRenderer))]
public class CustomEntryRenderer : EntryRenderer
{
public CustomEntryRenderer()
{
HideKeyboard();
}
void HideKeyboard()
{
this.Control.InputType = 0;
InputMethodManager inputMethodManager = this.Control.Context.GetSystemService(Context.InputMethodService) as InputMethodManager;
inputMethodManager.HideSoftInputFromWindow(this.Control.WindowToken, HideSoftInputFlags.ImplicitOnly);
}
// ...
}
* First create a derivated class from Entry
public class KBLessEntry : Entry
{
public KBLessEntry() : base()
{
}
}
* Then create a custom platform EntryRender
using Xamarin.Forms.Platform.Android;
using Xamarin.Forms;
using MobileClients.Droid.Core;
using Android.Views.InputMethods;
using System;
using System.ComponentModel;
[assembly: ExportRenderer(typeof(KBLessEntry), typeof(KBLessEntryRender))]
namespace MobileClients.Droid.Core
{
public class KBLessEntryRender : EntryRenderer
{
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
Control.InputType = 0;
try
{
// Hide keyboard
InputMethodManager inputMethodManager = this.Control.Context.GetSystemService(Android.Content.Context.InputMethodService) as InputMethodManager;
if (inputMethodManager != null)
{
inputMethodManager.HideSoftInputFromWindow(this.Control.WindowToken, HideSoftInputFlags.None);
}
}
catch(Exception Ex)
{
}
}
}
}
和XAML
<local:KBLessEntry x:Name="TxtCode" FontSize="18" Placeholder="Código producto" TextColor="Black" HorizontalOptions="FillAndExpand"></local:KBLessEntry>
local:必须在你的xaml中定义一个命名空间xmlns:local="clr-namespace:MobileClients. droid . core;assembly=MobileClients. "派生的Entry类(在本例中是KBLessEntry)所在的Droid
就这些
View view = ActivityCreateAccount.this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)
getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}