From e581869ebae0c3764a738e2906c030fd1ef7c3bc Mon Sep 17 00:00:00 2001
From: melimion <33512073+melimion@users.noreply.github.com>
Date: Thu, 21 Nov 2019 18:22:40 +0300
Subject: [PATCH] Fix [Android] Can not set Entry cursor position by tapping if
ClearButtonVisibility=WhenEditing (#8429)
* repro
* fix
* set _clearBtn to null in Dispose
* comment removed
---
.../Issue7773.cs | 34 ++++++++++++++++++++++
.../Xamarin.Forms.Controls.Issues.Shared.projitems | 1 +
.../Renderers/EntryRenderer.cs | 24 +++++++++++++--
3 files changed, 56 insertions(+), 3 deletions(-)
create mode 100644 Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7773.cs
diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7773.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7773.cs
new file mode 100644
index 0000000..6401197
--- /dev/null
+++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7773.cs
@@ -0,0 +1,34 @@
+using Xamarin.Forms.CustomAttributes;
+using Xamarin.Forms.Internals;
+
+namespace Xamarin.Forms.Controls.Issues
+{
+ [Preserve(AllMembers = true)]
+ [Issue(IssueTracker.Github, 7773, "[Android] Can not set Entry cursor position by tapping if ClearButtonVisibility=WhenEditing", PlatformAffected.Android)]
+ public class Issue7773 : TestContentPage
+ {
+ protected override void Init()
+ {
+ var stack = new StackLayout
+ {
+ Children = {
+ new Entry
+ {
+ Margin = new Thickness(50),
+ HorizontalOptions = LayoutOptions.FillAndExpand,
+ ClearButtonVisibility = ClearButtonVisibility.WhileEditing,
+ Text = "ClearButtonVisibility"
+ },
+ new Entry
+ {
+ HorizontalOptions = LayoutOptions.FillAndExpand,
+ ClearButtonVisibility = ClearButtonVisibility.WhileEditing,
+ Text = "ClearButtonVisibility2"
+ },
+ }
+ };
+
+ Content = stack;
+ }
+ }
+}
\ No newline at end of file
diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems
index 1ab2bfb..c002b24 100644
--- a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems
+++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems
@@ -21,6 +21,7 @@
+
diff --git a/Xamarin.Forms.Platform.Android/Renderers/EntryRenderer.cs b/Xamarin.Forms.Platform.Android/Renderers/EntryRenderer.cs
index e2a1db5..a441640 100644
--- a/Xamarin.Forms.Platform.Android/Renderers/EntryRenderer.cs
+++ b/Xamarin.Forms.Platform.Android/Renderers/EntryRenderer.cs
@@ -214,6 +214,7 @@ namespace Xamarin.Forms.Platform.Android
formsEditContext.SelectionChanged -= SelectionChanged;
ListenForCloseBtnTouch(false);
}
+ _clearBtn = null;
}
base.Dispose(disposing);
@@ -530,6 +531,7 @@ namespace Xamarin.Forms.Platform.Android
// Entry clear button management
public abstract partial class EntryRendererBase
{
+ Drawable _clearBtn;
internal override void OnNativeFocusChanged(bool hasFocus)
{
base.OnNativeFocusChanged(hasFocus);
@@ -544,9 +546,24 @@ namespace Xamarin.Forms.Platform.Android
void EditTextTouched(object sender, TouchEventArgs e)
{
- MotionEvent me = e.Event;
- if (me.Action == MotionEventActions.Up && me.RawX >= EditText.Right - EditText.CompoundPaddingRight)
- EditText.Text = null;
+ e.Handled = false;
+ var me = e.Event;
+
+ var rBounds = _clearBtn?.Bounds;
+ if (rBounds != null)
+ {
+ var x = me.GetX();
+ var y = me.GetY();
+ if (me.Action == MotionEventActions.Up
+ && x >= (EditText.Right - rBounds.Width())
+ && x <= (EditText.Right - EditText.PaddingRight)
+ && y >= EditText.PaddingTop
+ && y <= (EditText.Height - EditText.PaddingBottom))
+ {
+ EditText.Text = null;
+ e.Handled = true;
+ }
+ }
}
void UpdateClearBtnOnElementChanged()
@@ -591,6 +608,7 @@ namespace Xamarin.Forms.Platform.Android
{
Drawable d = showClearButton && (Element.Text?.Length > 0) ? GetCloseButtonDrawable() : null;
EditText.SetCompoundDrawablesWithIntrinsicBounds(null, null, d, null);
+ _clearBtn = d;
}
protected virtual Drawable GetCloseButtonDrawable()
--
2.7.4