在自定义渲染器中更改Xamarin.Forms.Entry的键盘的ImeOptions在Android上不起作用 [英] Changing keyboard's ImeOptions of Xamarin.Forms.Entry in custom renderer not working on Android
问题描述
我所拥有的:
我有一个从Xamarin.Forms.Entry
派生的自定义类MyEntry
和一个用于Android和iOS的自定义渲染器类MyEntryRenderer
.
I have a custom class MyEntry
derived from Xamarin.Forms.Entry
and custom renderer classes MyEntryRenderer
for Android and iOS.
我想要的东西:
我想通过更改Android
上的ImeOptions
和iOS
上的ReturnKeyType
将键盘的输入"按钮更改为搜索"按钮(请参见示例代码).当我按下更改的搜索"按钮时,应该调用MyEntry.Completed
事件(就像在我按下未更改的输入"按钮之前一样.
I want to change the keyboard's "enter"-button to a "search"-button by changing ImeOptions
on Android
and ReturnKeyType
on iOS
(see sample code). When I press the altered "search"-button, the MyEntry.Completed
event should be called (like before when I pressed the un-altered "enter"-button.
实际发生的事情:
在iOS
上,代码按预期工作.但是在Android
上什么也没有发生.该事件不会被调用.
On iOS
the code works like expected. But on Android
nothing happens. The event doesn't get called.
我的问题:
如何实现上面在Android
上描述的内容?
How can I achieve what I described above on Android
?
示例代码:
App.cs:
namespace CustomEntry
{
public class App
{
public static Page GetMainPage()
{
MyEntry entry = new MyEntry {
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.CenterAndExpand,
Placeholder = "Enter some text"
};
entry.Completed += delegate {
Console.WriteLine("Completed");
};
return new ContentPage {
Content = entry,
};
}
}
}
MyEntry.cs:
MyEntry.cs:
namespace CustomEntry
{
public class MyEntry:Entry
{
}
}
MyEntryRenderer.cs(Android):
MyEntryRenderer.cs (Android):
[assembly: ExportRenderer(typeof(MyEntry), typeof(MyEntryRenderer))]
namespace CustomEntry.Android
{
public class MyEntryRenderer:EntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (Control != null) {
Control.ImeOptions = global::Android.Views.InputMethods.ImeAction.Search;
}
}
}
}
MyEntryRenderer.cs(iOS):
MyEntryRenderer.cs (iOS):
[assembly: ExportRenderer(typeof(MyEntry), typeof(MyEntryRenderer))]
namespace CustomEntry.iOS
{
public class MyEntryRenderer:EntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (Control != null) {
Control.ReturnKeyType = UIReturnKeyType.Search;
}
}
}
}
推荐答案
我自己找到了解决问题的方法:
首先,我在我的自定义条目中添加了Action
,当我按下搜索"按钮时将调用它.
First I added an Action
to my custom entry to be called when I press my "search"-button.
MyEntry.cs
MyEntry.cs
namespace CustomEntry
{
public class MyEntry:Entry
{
public Action SearchPressed = delegate {
};
}
}
第二,我像这样监听" ImeAction.Search
并调用添加到自定义条目中的Action
.
Second I "listen" for ImeAction.Search
like this and call the Action
I added to my custom entry.
MyEntryRenderer.cs(Android):
MyEntryRenderer.cs (Android):
[assembly: ExportRenderer(typeof(MyEntry), typeof(MyEntryRenderer))]
namespace CustomEntry.Android
{
public class MyEntryRenderer:EntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (Control != null) {
Control.ImeOptions = ImeAction.Search;
Control.EditorAction += (sender, args) => {
if (args.ActionId == ImeAction.Search) {
var entry = (AddressEntry)Element;
entry.SearchPressed();
}
};
}
}
}
}
在使用MyEntry
的第三类中,当按下搜索"按钮时,我可以运行一些代码:
In a third class where I use MyEntry
I can run some code when the "search"-button is pressed like this:
var myEntry = new MyEntry();
myEntry.SearchPressed += SomeMethod;
这篇关于在自定义渲染器中更改Xamarin.Forms.Entry的键盘的ImeOptions在Android上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!