isPasswordInputScopeContainer.ContainerLayout.Children.Add(switchPasswordButton);
isPasswordInputScopeContainer.ContainerLayout.Children.Add(switchNumericButton);
+ var switchClearBtnVisibilityBtn = new Button { Text = "Toggle ClearButtonVisibility" };
+ var clearBtnModelContainer = new ViewContainer<Entry>(Test.Entry.ClearButtonVisibility,
+ new Entry { Text = "I should have clear button visible", ClearButtonVisibility = ClearButtonVisibility.WhileEditing });
+ switchClearBtnVisibilityBtn.Clicked += (o, a) =>
+ clearBtnModelContainer.View.ClearButtonVisibility = clearBtnModelContainer.View.ClearButtonVisibility == ClearButtonVisibility.Never ? ClearButtonVisibility.WhileEditing : ClearButtonVisibility.Never;
+ clearBtnModelContainer.ContainerLayout.Children.Add(switchClearBtnVisibilityBtn);
+
Add (isPasswordContainer);
Add (completedContainer);
Add (placeholderContainer);
Add (maxLengthContainer);
Add (readOnlyContainer);
Add (isPasswordInputScopeContainer);
+ Add (clearBtnModelContainer);
}
}
}
\ No newline at end of file
-using System;
-using System.Collections.Generic;
-using System.Text;
+using System.Linq;
namespace Xamarin.Forms.Controls.GalleryPages
{
public MaterialEntryGalleryPage()
{
Visual = VisualMarker.Material;
- Content =
- new ScrollView()
- {
- Content = new StackLayout()
+ StackLayout layout = null;
+ layout = new StackLayout()
+ {
+ Children =
+ {
+ new Entry()
+ {
+ Text = "With Text",
+ Placeholder = "Placeholder",
+ },
+ new Entry()
+ {
+ Placeholder = "Placeholder"
+ },
+ new Entry()
+ {
+ Placeholder = "Green Placeholder",
+ PlaceholderColor = Color.Green,
+ },
+ new Entry()
+ {
+ Placeholder = "Purple Placeholder",
+ PlaceholderColor = Color.Purple,
+ },
+ new Entry()
+ {
+ Text = "Green TextColor",
+ TextColor = Color.Green
+ },
+ new Entry()
+ {
+ Text = "Purple TextColor",
+ TextColor = Color.Purple
+ },
+ new Entry()
+ {
+ Text = "With Text larger font",
+ Placeholder = "Placeholder",
+ FontSize = 24
+ },
+ new Entry()
+ {
+ Text = "Yellow BackgroundColor",
+ BackgroundColor = Color.Yellow
+ },
+ new Entry()
{
- Children =
+ Text = "Cyan BackgroundColor",
+ BackgroundColor = Color.Cyan
+ },
+ new Button()
+ {
+ Text = "Toggle Entry clear button mode",
+ Command = new Command(() =>
{
- new Entry()
- {
- Text = "With Text",
- Placeholder="Placeholder",
- },
- new Entry()
- {
- Placeholder = "Placeholder"
- },
- new Entry()
- {
- Placeholder = "Green Placeholder",
- PlaceholderColor = Color.Green,
- },
- new Entry()
- {
- Placeholder = "Purple Placeholder",
- PlaceholderColor = Color.Purple,
- },
- new Entry()
- {
- Text = "Green TextColor",
- TextColor = Color.Green
- },
- new Entry()
- {
- Text = "Purple TextColor",
- TextColor = Color.Purple
- },
- new Entry()
- {
- Text = "With Text larger font",
- Placeholder="Placeholder",
- FontSize = 24
- },
- new Entry()
- {
- Text = "Yellow BackgroundColor",
- BackgroundColor = Color.Yellow
- },
- new Entry()
+ foreach(Entry e in layout.Children.OfType<Entry>())
{
- Text = "Cyan BackgroundColor",
- BackgroundColor = Color.Cyan
- },
- }
+ e.ClearButtonVisibility = e.ClearButtonVisibility == ClearButtonVisibility.Never ? ClearButtonVisibility.WhileEditing : ClearButtonVisibility.Never;
+ }
+ })
}
+ }
+ };
- };
+ Content = new ScrollView()
+ {
+ Content = layout
+ };
}
}
}
Assert.AreEqual(eventLabelText, "Event: Completed (fired 1)");
}
+ [Test]
+ [UiTest(typeof(Entry), "ClearButtonVisibility")]
+ [Category(UITestCategories.ManualReview)]
+ public void ClearButtonVisibility()
+ {
+ var remote = new StateViewContainerRemote(App, Test.Entry.ClearButtonVisibility, PlatformViewType);
+ remote.GoTo();
+
+ App.WaitForElement(q => q.Marked("Toggle ClearButtonVisibility"));
+ App.Tap(q => q.Marked("Toggle ClearButtonVisibility"));
+ }
+
protected override void FixtureTeardown()
{
App.NavigateBack();
public static readonly BindableProperty SelectionLengthProperty = BindableProperty.Create(nameof(SelectionLength), typeof(int), typeof(Entry), 0, validateValue: (b, v) => (int)v >= 0);
+ public static readonly BindableProperty ClearButtonVisibilityProperty = BindableProperty.Create(nameof(ClearButtonVisibility), typeof(ClearButtonVisibility), typeof(Entry), ClearButtonVisibility.Never);
+
readonly Lazy<PlatformConfigurationRegistry<Entry>> _platformConfigurationRegistry;
public Entry()
get { return (bool)GetValue(IsTextPredictionEnabledProperty); }
set { SetValue(IsTextPredictionEnabledProperty, value); }
}
-
+
public ReturnType ReturnType
{
get => (ReturnType)GetValue(ReturnTypeProperty);
set => SetValue(ReturnCommandParameterProperty, value);
}
+ public ClearButtonVisibility ClearButtonVisibility
+ {
+ get => (ClearButtonVisibility)GetValue(ClearButtonVisibilityProperty);
+ set => SetValue(ClearButtonVisibilityProperty, value);
+ }
+
double IFontElement.FontSizeDefaultValueCreator() =>
Device.GetNamedSize(NamedSize.Default, (Entry)this);
set { SetValue(IsReadOnlyProperty, value); }
}
}
+
+ public enum ClearButtonVisibility
+ {
+ Never,
+ WhileEditing
+ }
}
\ No newline at end of file
PasswordColor,
MaxLength,
IsReadOnly,
- IsPasswordNumeric
+ IsPasswordNumeric,
+ ClearButtonVisibility
}
public enum Frame
using System.Collections.Generic;
using System.ComponentModel;
using Android.Content;
+using Android.Graphics.Drawables;
+using Android.Support.V4.Content;
using Android.Text;
using Android.Text.Method;
using Android.Util;
}
}
- public abstract class EntryRendererBase<TControl> : ViewRenderer<Entry, TControl>, ITextWatcher, TextView.IOnEditorActionListener
+ public abstract partial class EntryRendererBase<TControl> : ViewRenderer<Entry, TControl>, ITextWatcher, TextView.IOnEditorActionListener
where TControl : global::Android.Views.View
{
bool _disposed;
void ITextWatcher.AfterTextChanged(IEditable s)
{
+ OnAfterTextChanged(s);
}
void ITextWatcher.BeforeTextChanged(ICharSequence s, int start, int count, int after)
if (_cursorPositionChangePending || _selectionLengthChangePending)
UpdateCursorSelection();
+
+ UpdateClearBtnOnElementChanged();
}
protected override void Dispose(bool disposing)
{
formsEditContext.OnKeyboardBackPressed -= OnKeyboardBackPressed;
formsEditContext.SelectionChanged -= SelectionChanged;
+ ListenForCloseBtnTouch(false);
}
}
UpdateCursorSelection();
else if (e.PropertyName == InputView.IsReadOnlyProperty.PropertyName)
UpdateIsReadOnly();
+ if (e.PropertyName == Entry.ClearButtonVisibilityProperty.PropertyName)
+ UpdateClearBtnOnPropertyChanged();
base.OnElementPropertyChanged(sender, e);
}
EditText.Focusable = isReadOnly;
}
}
+
+ // Entry clear button management
+ public abstract partial class EntryRendererBase<TControl>
+ {
+ internal override void OnNativeFocusChanged(bool hasFocus)
+ {
+ base.OnNativeFocusChanged(hasFocus);
+ UpdateClearBtnOnFocusChanged(hasFocus);
+ }
+
+ void OnAfterTextChanged(IEditable s)
+ {
+ if (Control.IsFocused)
+ UpdateClearBtnOnTyping();
+ }
+
+ void EditTextTouched(object sender, TouchEventArgs e)
+ {
+ MotionEvent me = e.Event;
+ if (me.Action == MotionEventActions.Up && me.RawX >= EditText.Right - EditText.CompoundPaddingRight)
+ EditText.Text = null;
+ }
+
+ void UpdateClearBtnOnElementChanged()
+ {
+ bool showClearBtn = Element.ClearButtonVisibility == ClearButtonVisibility.WhileEditing;
+ if (showClearBtn && Element.IsFocused)
+ {
+ UpdateClearBtn(true);
+ ListenForCloseBtnTouch(true);
+ }
+ }
+
+ void UpdateClearBtnOnPropertyChanged()
+ {
+ bool isFocused = Element.IsFocused;
+ if (isFocused)
+ {
+ bool showClearBtn = Element.ClearButtonVisibility == ClearButtonVisibility.WhileEditing;
+ UpdateClearBtn(showClearBtn);
+
+ if (!showClearBtn && isFocused)
+ ListenForCloseBtnTouch(false);
+ }
+ }
+
+ void UpdateClearBtnOnFocusChanged(bool isFocused)
+ {
+ if (Element.ClearButtonVisibility == ClearButtonVisibility.WhileEditing)
+ {
+ UpdateClearBtn(isFocused);
+ ListenForCloseBtnTouch(isFocused);
+ }
+ }
+
+ void UpdateClearBtnOnTyping()
+ {
+ if (Element.ClearButtonVisibility == ClearButtonVisibility.WhileEditing)
+ UpdateClearBtn(true);
+ }
+
+ void UpdateClearBtn(bool showClearButton)
+ {
+ Drawable d = showClearButton && (Element.Text?.Length > 0) ? GetCloseButtonDrawable() : null;
+ EditText.SetCompoundDrawablesWithIntrinsicBounds(null, null, d, null);
+ }
+
+ protected virtual Drawable GetCloseButtonDrawable()
+ => ContextCompat.GetDrawable(Context, Resource.Drawable.abc_ic_clear_material);
+
+ void ListenForCloseBtnTouch(bool listen)
+ {
+ if (listen)
+ EditText.Touch += EditTextTouched;
+ else
+ EditText.Touch -= EditTextTouched;
+ }
+ }
}
UpdateCursorColor();
UpdateIsReadOnly();
+
+ if (Element.ClearButtonVisibility != ClearButtonVisibility.Never)
+ UpdateClearButtonVisibility();
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
UpdateCursorColor();
else if (e.PropertyName == Xamarin.Forms.InputView.IsReadOnlyProperty.PropertyName)
UpdateIsReadOnly();
+ else if (e.PropertyName == Entry.ClearButtonVisibilityProperty.PropertyName)
+ UpdateClearButtonVisibility();
base.OnElementPropertyChanged(sender, e);
}
}
}
- void UpdateIsReadOnly()
- {
- Control.UserInteractionEnabled = !Element.IsReadOnly;
- }
- }
+ void UpdateIsReadOnly()
+ {
+ Control.UserInteractionEnabled = !Element.IsReadOnly;
+ }
+
+ void UpdateClearButtonVisibility()
+ {
+ Control.ClearButtonMode = Element.ClearButtonVisibility == ClearButtonVisibility.WhileEditing ? UITextFieldViewMode.WhileEditing : UITextFieldViewMode.Never;
+ }
+ }
}