Android Edit Text Disguise

I want to add a disguise .. like 00000-0000000-0

etusercnic.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { try { String str = s.toString(); if (s.length() == 5 || s.length() == 13) { str += "-"; etusercnic.setText(str); etusercnic.setSelection(str.length()); } } catch (Exception ignored) { } } @Override public void afterTextChanged(Editable s) { } }); 

It works great when I first entered a value, but when I delete any digit, it is placed - a sign. so what can i do.

+5
source share
3 answers

I'm sure you are masking it for CNIC: D

In any case, this is my disguise version, it works fine:

  etusercnic.setRawInputType(InputType.TYPE_CLASS_NUMBER); etusercnic.addTextChangedListener(new TextWatcher() { int len = 0; @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub String val = etusercnic.getText().toString(); if((val.length()==5 && len <val.length()) || (val.length()==13 && len<val.length())){ etusercnic.append("-"); } } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { String str = etusercnic.getText().toString(); len = str.length(); } @Override public void afterTextChanged(Editable s) { // TODO Auto-generated method stub } }); 

The xml of your Edittext should look something like this:

  <EditText android:id="@+id/nic_field" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/signup_cnic_hint" android:singleLine="true" android:inputType="date" android:maxLength="15" android:textColor="#000000" android:textColorHint="#808080" android:textSize="15sp" /> 
+3
source

Good luck with your project! The easiest way to use a mask on EditText in your Android programs in Android Studio is to use the MaskedEditText library ( GitHub link ). This is a kind of custom EditText with Watcher that allows you to set a tooltip with a different color (if you want it to be available even when the user has already started typing), the mask is very easy to use :-)

In your case, it is very simple:

 compile 'ru.egslava:MaskedEditText:1.0.5' <br.com.sapereaude.maskedEditText.MaskedEditText android:id="@+id/phone_input" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="phone" android:typeface="monospace" mask:allowed_chars="1234567890" mask:mask="#####-#######-#" android:hint="0000000000000" app:keep_hint="true" /> 

And this! Good luck

enter image description here

0
source

After some help .. I found a way. The correct answer ... Perfect work. :)

  etusercnic.addTextChangedListener(new TextWatcher() { int len = 0; @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { String str = etusercnic.getText().toString(); len = str.length(); } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { try { String str = s.toString(); String val = etusercnic.getText().toString(); if ((val.length() == 5 && len < val.length()) || (val.length() == 13 && len < val.length())) { str += "-"; etusercnic.setText(str); etusercnic.setSelection(str.length()); } } catch (Exception ignored) { } } @Override public void afterTextChanged(Editable s) { } }); 
-1
source

Source: https://habr.com/ru/post/1238100/


All Articles