2015-12-14 45 views
5

我要添加掩蔽.. 像00000-0000000-0Android设备上编辑文本掩蔽

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) { 
     } 
    }); 

它完美,当我第一次输入的值,但是当我删除任何数字它放在 - 标志。所以我可以做什么..

+0

法赫德学会[接受答案](http://stackoverflow.com/help/someone-answers)上叠层上流动。如果它适合你,如果它不起作用,那么请评论你正面临的问题。 –

回答

3

我敢肯定你掩盖它CNIC:d

反正这里是我的版本掩蔽,它完美的作品:

 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 

     } 
    }); 

的XML你的Edittext应该是这样的:

<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" 
    /> 
+0

etusercnic.append(“ - ”);不工作。结果是 - 000000000000 –

+0

@fahadkhalid你能接受我的回答吗plz –

+0

谢谢你的帮助.. Sharp Edge –

-1

经过一些帮助..我找到了一种方法。正确答案...完美工作。 :)

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) { 
     } 
    }); 
0

祝您的项目顺利!我知道在Android Studio的Android程序的EditText上使用蒙版最简单的方法是使用MaskedEditText库(GitHub link)。 这是一种自定义的EditText与看守,使您可以设置不同颜色的提示(如果你想,如果将可用,即使用户已经开始键入),面膜,它是很容易:-)

使用这很容易在你的情况下:

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" 
    /> 

这就是!祝你好运!

enter image description here