我们正在尝试使用Xamarin Forms构建聊天应用程序,但是我们一直在使用Android键盘来获取此烦人的bug。每当点击“发送”按钮时,对输入(用于聊天的文本框)的焦点就会丢失,并且键盘也会消失。那不是我们想要的,因此我们在TapGestureRecognizer中添加了以下行:
messageEntry.Focus();
但是由于某种原因,这种情况发生得不够快,并且通常键盘会掉下来并立即又起来。这样可以防止用户快速按顺序发布多条消息。有人知道该如何解决吗?
最佳答案
感谢此post中@AdamKemp的回答,这是我的解决方案。如果触摸在我的EntryStackLayout
内(不要忘记创建空的自定义渲染器),那么我就不会关闭键盘(如果DispatchTouchEvent
是CurrentFocus
,这就是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;