On my page, I'm using a picker that bound an ItemSource to 3 selectable items. When I select something from the list or cancel it, the focus stays on the picker and that allows me to type something. Although this does not affect the value selected, it can be confusing to the user Is there an easy way to prevent a user from entering data in this field?
CodePudding user response:
Solution (or a work around) is disabling focus using a custom renderer:
internal class CustomPicker : PickerRenderer
{
public CustomPicker(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
{
base.OnElementChanged(e);
EditText?.SetFocusable(ViewFocusability.NotFocusable);
}
}
CodePudding user response:
I have done a sample to test and the picker's text will be changed if user tap the the physical keyboard, but the soft keyboard will not show.
If you don't want the picker accept the input from the user, you can use the coustom render just like Cfun said. You can just set the Focuseable property as false and then the picker will not accept the input from keyboard.
public class MyPickerRender : PickerRenderer
{
public MyPickerRender(Context context) : base(context)
{
}
protected override void OnElementChanged(Xamarin.Forms.Platform.Android.ElementChangedEventArgs<Picker> e)
{
base.OnElementChanged(e);
Control.Focusable = false;
}
}
CodePudding user response:
Both approaches seem to solve the problem. Thanks for your help and time.