Reputation: 157
I have a problem changing the text of "EditText" in "Xamarin Android" ,I wish to put a "/" after 2 character's so that ,I can obtain text like "MM/YY"
It happens to Work with if condition but when user presses "backspace" to erase it ,text does not vanishes, I have so far divided those four character's with "Replace" Function but am unable to put that "str" back to the Edit Text, If i do so , It simply Crashes.
Code -TextChangedEvent of expiryEditText
var Expirydata = expiryEditText.Text;
if(Expirydata.Length == 2)
{
expiryEditText.Text = expiryEditText.Text + "/";
}
else if(Expirydata.Length ==5)
{
monthId = Expirydata.Substring(0, 2);
yearId = Expirydata.Substring(Expirydata.Length - 2);
var xx = monthId + "/"+ yearId;
String str = Expirydata.ToString().Replace(Expirydata,xx);
// expiryEditText.Text = xx.ToString();
}
else
{
}
Upvotes: 1
Views: 1178
Reputation: 16519
The reason this is happening is that your current code is not considering the part where the user can press backspace to get back to remove the text again for that you can use the Start property to find out what the current value is and make the changes accordingly
if (expiryEditText.Length() == 2 && e.Start != 2)
{
expiryEditText.Append("/");
}
else
{
if (expiryEditText.Length() >2 && expiryEditText.Text.IndexOf('-') == -1)
{
expiryEditText.Text = expiryEditText.Text.Insert(2, "-");
expiryEditText.SetSelection(expiryEditText.Text.Length);
}
}
Good luck
Revert in case of queries.
Upvotes: 1