In Xamarin.Android , you can try to handle the edit text key press event. Keycode is Keycode.Back when the delete button is pressed on a keyboard and handle the event accordingly.
editText.KeyPress += (object sender, View.KeyEventArgs e) => {
e.Handled = false;
if (e.Event.Action == KeyEventActions.Down && e.KeyCode == Keycode.Back)
{
//your logic here
e.Handled = true;
}
};
In Xamarin.iOS
You can try overriding the DeleteBackward method in the custom text field (BackDeleteEventTextField) and call a custom event OnDeleteBackwardKeyPressed.
In the custom renderer you can override the text field with your custom text field in on element changed method
var textField = new BackDeleteEventTextField();
and handle the custom event OnDeleteBackwardKeyPressed
textField.OnDeleteBackwardKeyPressed += (sender, a) =>
{
//handle the back key pressed event
};
In Xamarin.Android :
In the custom renderer you can handle the key press event and look for the Keycode.Back
((EditText)this.Control).KeyPress += (object sender, View.KeyEventArgs even) => {
even.Handled = false;
if (even.Event.Action == KeyEventActions.Down && even.KeyCode == Keycode.Back)
{
//your logic here even.Handled = true;
}
};
Elaborating for xamarin.iOS using forms.
Step 1: Create a custom entry class and create a delegate to handle the back button press
public class CustomEntry: Entry
{
public delegate void BackButtonPressEventHandler(object sender, EventArgs e);
public event BackButtonPressEventHandler OnBackButton;
public CustomEntry() { }
public void OnBackButtonPress()
{
if (OnBackButton!= null)
{
OnBackButton(null, null);
}
}
}
Step 2 :Within the custom renderer namespace , create a custom text field class.
[assembly: ExportRenderer(typeof(CustomEntry), typeof(CustomEntryRenderer))]
namespace Origination.iOS.Renderers
{
public class CustomTextField: UITextField
{
}
}
Step 3: Within the custom text field, create a event and delegate to handle the delete button click.
public class CustomTextField: UITextField
{
// A delegate type for hooking up change notifications.
public delegate void DeleteBackwardKeyEventHandler(object sender, EventArgs e);
// An event that clients can use to be notified whenever the
// elements of the list change.
public event DeleteBackwardKeyEventHandler OnDeleteBackwardKey;
public void OnDeleteBackwardKeyPressed()
{
if (OnDeleteBackwardKey != null)
{
OnDeleteBackwardKey(null, null);
}
}
public override void DeleteBackward()
{
base.DeleteBackward();
OnDeleteBackwardKeyPressed();
}
}
Step 4 :Within the custom renderer name space, Create custom renderer class.
public class CustomEntryRenderer: EntryRenderer, IUITextFieldDelegate
{
}
Step 5:Within the custom renderer's OnElementChanged method create a text field of the custom text field type.
Step 6: Handle the custom text field delete event by passing it to the custom entry back button event handler.
Step 7: Assign the custom text field object to native control.
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
if (Element == null)
{
return;
}
var entry = (CustomEntry)Element;
var textField = new CustomTextField();
textField.EditingChanged += OnEditingChanged;
textField.OnDeleteBackwardKey += (sender, a) =>
{
entry.OnBackButtonPress();
};
SetNativeControl(textField);
base.OnElementChanged(e);
}
step 8 : Add the editing changed handler
IElementController ElementController => Element as IElementController;
void OnEditingChanged(object sender, EventArgs eventArgs)
{
ElementController.SetValueFromRenderer(Entry.TextProperty, Control.Text);
}
In Xamarin.Android , you can try to handle the edit text key press event. Keycode is Keycode.Back when the delete button is pressed on a keyboard and handle the event accordingly.
editText.KeyPress += (object sender, View.KeyEventArgs e) => {
e.Handled = false;
if (e.Event.Action == KeyEventActions.Down && e.KeyCode == Keycode.Back)
{
//your logic here
e.Handled = true;
}
};
In Xamarin.iOS
You can try overriding the DeleteBackward method in the custom text field (BackDeleteEventTextField) and call a custom event OnDeleteBackwardKeyPressed.
In the custom renderer you can override the text field with your custom text field in on element changed method
var textField = new BackDeleteEventTextField();
and handle the custom event OnDeleteBackwardKeyPressed
textField.OnDeleteBackwardKeyPressed += (sender, a) =>
{
//handle the back key pressed event
};
In Xamarin.Android :
In the custom renderer you can handle the key press event and look for the Keycode.Back
((EditText)this.Control).KeyPress += (object sender, View.KeyEventArgs even) => {
even.Handled = false;
if (even.Event.Action == KeyEventActions.Down && even.KeyCode == Keycode.Back)
{
//your logic here even.Handled = true;
}
};
**Update**
Elaborating for xamarin.iOS using forms.
Step 1:
Create a custom entry class and create a delegate to handle the back button press
public class CustomEntry: Entry
{
public delegate void BackButtonPressEventHandler(object sender, EventArgs e);
public event BackButtonPressEventHandler OnBackButton;
public CustomEntry() { }
public void OnBackButtonPress()
{
if (OnBackButton!= null)
{
OnBackButton(null, null);
}
}
}
Step 2 :Within the custom renderer namespace , create a custom text field class.
[assembly: ExportRenderer(typeof(CustomEntry), typeof(CustomEntryRenderer))]
namespace Origination.iOS.Renderers
{
public class CustomTextField: UITextField
{
}
}
Step 3: Within the custom text field, create a event and delegate to handle the delete button click.
public class CustomTextField: UITextField
{
// A delegate type for hooking up change notifications.
public delegate void DeleteBackwardKeyEventHandler(object sender, EventArgs e);
// An event that clients can use to be notified whenever the
// elements of the list change.
public event DeleteBackwardKeyEventHandler OnDeleteBackwardKey;
public void OnDeleteBackwardKeyPressed()
{
if (OnDeleteBackwardKey != null)
{
OnDeleteBackwardKey(null, null);
}
}
public override void DeleteBackward()
{
base.DeleteBackward();
OnDeleteBackwardKeyPressed();
}
}
Step 4 :Within the custom renderer name space, Create custom renderer class.
public class CustomEntryRenderer: EntryRenderer, IUITextFieldDelegate
{
}
Step 5:Within the custom renderer's OnElementChanged method create a text field of the custom text field type.
Step 6: Handle the custom text field delete event by passing it to the custom entry back button event handler.
Step 7: Assign the custom text field object to native control.
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
if (Element == null)
{
return;
}
var entry = (CustomEntry)Element;
var textField = new CustomTextField();
textField.EditingChanged += OnEditingChanged;
textField.OnDeleteBackwardKey += (sender, a) =>
{
entry.OnBackButtonPress();
};
SetNativeControl(textField);
base.OnElementChanged(e);
}
step 8 : Add the editing changed handler
IElementController ElementController => Element as IElementController;
void OnEditingChanged(object sender, EventArgs eventArgs)
{
ElementController.SetValueFromRenderer(Entry.TextProperty, Control.Text);
}