我们正在尝试使用Xamarin Forms构建聊天应用程序,但我们一直在使用Android键盘来解决这个烦人的错误。每当"发送"按钮被按下,条目(聊天的文本框)上的焦点丢失,键盘消失。这不是我们想要的,所以我们将这一行添加到TapGestureRecognizer中:
messageEntry.Focus();
但由于某种原因,这种情况发生得不够快,通常键盘会立即关闭并立即再次启动。这可以防止用户按顺序快速发布多条消息。有人知道如何解决这个问题吗?
答案 0 :(得分:1)
感谢@ post中@AdamKemp的回答,这是我的解决方案。如果触摸位于我的EntryStackLayout
范围内(不要忘记创建空的自定义渲染器),那么我就不会忽略键盘(如果{DispatchTouchEvent
CurrentFocus
将会执行此操作1}}是EditText
)。
public class EditorAndButtonReproPage : ContentPage
{
public EditorAndButtonReproPage()
{
BackgroundColor = Color.Gray;
Padding = 50;
var editor = new Editor {HorizontalOptions = LayoutOptions.FillAndExpand};
var editorButton = new Button {Text = "OK", HorizontalOptions = LayoutOptions.End};
var editorLayout = new EntryStackLayout { Orientation = StackOrientation.Horizontal, Children = { editor, editorButton}, VerticalOptions = LayoutOptions.Start};
var entry = new ExtendedEntry { Placeholder = "Entry", HorizontalOptions = LayoutOptions.FillAndExpand };
var entryButton = new Button { Text = "OK", HorizontalOptions = LayoutOptions.End };
var entryLayout = new EntryStackLayout { Orientation = StackOrientation.Horizontal, Children = { entry, entryButton }, VerticalOptions = LayoutOptions.Start };
Content = new StackLayout {Children = {editorLayout, entryLayout}};
}
}
和MainActivity
:
private bool _ignoreNewFocus;
public override bool DispatchTouchEvent(MotionEvent e)
{
var currentView = CurrentFocus;
var parent = currentView?.Parent?.Parent;
var entryStackLayout = parent as EntryStackLayout;
if (entryStackLayout != null)
{
var entryLayoutLocation = new int[2];
entryStackLayout.GetLocationOnScreen(entryLayoutLocation);
var x = e.RawX + entryStackLayout.Left - entryLayoutLocation[0];
var y = e.RawY + entryStackLayout.Top - entryLayoutLocation[1];
var entryStackLayoutRect = new Rectangle(entryStackLayout.Left, entryStackLayout.Top, entryStackLayout.Width, entryStackLayout.Height);
_ignoreNewFocus = entryStackLayoutRect.Contains(x, y);
}
var result = base.DispatchTouchEvent(e);
_ignoreNewFocus = false;
return result;
}
public override Android.Views.View CurrentFocus => _ignoreNewFocus ? null : base.CurrentFocus;
答案 1 :(得分:0)
它有点像黑客攻击,但你可以分离出等待50ms的异步任务,然后在主UI线程上调用messageEntry.Focus()行...