当在输入“数值”文本框的时候,我们更希望on focus的时候全选编辑内容,以下给大家介绍如何实现
Xaml中定义Keyboard为Numeric
<local:CustomEntry WidthRequest="100" Keyboard="Numeric"/>
Android :
public class CustomEntryRenderer : EntryRenderer
{
public BorderEntryRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (Control != null)
{
//Keyboard.Numeric set select all onFocus
if (Control.InputType == (InputTypes.ClassNumber|InputTypes.NumberFlagDecimal|InputTypes.NumberFlagSigned))
{
Control.SetSelectAllOnFocus(true);
}
}
}
}
IOS:
public class CustomEntryRenderer: EntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (Control != null)
{
var nativeTextField = (UITextField)Control;
//if input type is numeric
if (nativeTextField.KeyboardType == UIKeyboardType.DecimalPad)
{
nativeTextField.EditingDidBegin += NativeTextField_EditingDidBegin;
}
}
}
private void NativeTextField_EditingDidBegin(object sender, EventArgs e)
{
((UITextField)sender).PerformSelector(new ObjCRuntime.Selector("selectAll"), null, 0.0f);
}
}