如何在Android中删除Dash表单电话号码格式XXX-XXX-XXXX

时间:2013-09-18 10:33:39

标签: android textchanged libphonenumber

有人可以帮我解决这个问题吗?我在电话号码之间添加了Dash( - )。 下面我提到了我的代码。

//从此处删除

现在我想在按下按钮时删除短划线( - )。就我而言,我无法做到这一点。 让我们说如果我插入3个字符,那么Dash会在这三个数字后自动添加,然后如果我按下后退按钮我就无法删除破折号,甚至是破折号之前的三个数字。

我必须选择所有文本,然后我必须按删除然后它将删除..但使用退格我不能删除.. 那我怎么能这样做呢。谁能给我代码答案呢???

提前致谢, JT。


从这里更新

public class MainActivity extends Activity {
        EditText inputPhone;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.create_new_member);
            inputPhone = (EditText) findViewById(R.id.phoneText);
            inputPhone.addTextChangedListener(new TextWatcher() {
                int len = 0;
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    // TODO Auto-generated method stub
                    String str = inputPhone.getText().toString();
                    if ((str.length() == 3 && len < str.length()) || (str.length() == 7 && len < str.length())) {
                        inputPhone.append("-");
                    }
                }
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    String str = inputPhone.getText().toString();
                    len = str.length();
                }
                @Override
                public void afterTextChanged(Editable s) {
                    // TODO Auto-generated method stub
                }
            });
        }
    }

1 个答案:

答案 0 :(得分:0)

按照这种方法解决你的问题

http://www.shaikhhamadali.blogspot.com/2013/06/how-to-apply-niccustom-format-to-edit.html

就像这个解决方案一样,你也应该在退格上填写文本的长度。

将此标记为其他人帮助的答案。

    //get the reference of this edit text field
     EditText  etNICNO_Sender=(EditText)findViewById(R.id.etNICNO_Sender);
        /*add textChangeListner with TextWatcher argument
            by adding text change listner with text watcher we can get three methods of
            Edit Text 1) onTextChanged 2) beforeTextChanged 3) afterTextChanged
            these methods work when user types in text feild.
         */
 etNICNO_Sender.addTextChangedListener(new TextWatcher() {
   int len=0;
   @Override
   public void onTextChanged(CharSequence s, int start, int before, int count) {
    // TODO Auto-generated method stub

  String str = etNICNO_Sender.getText().toString();

  if((str.length()==3 && len <str.length()) || (str.length()==7 && len <str.length())){
                  //checking length  for backspace.
                  etNICNO_Sender.append("-");
                  //Toast.makeText(getBaseContext(), "add minus", Toast.LENGTH_SHORT).show();
                 }
   }
   @Override
   public void beforeTextChanged(CharSequence s, int start, int count,
     int after) {
    // TODO Auto-generated method stub
    String str = etNICNO_Sender.getText().toString();
                         len = str.length();
        }
   @Override
   public void afterTextChanged(Editable s) {
    // TODO Auto-generated method stub

   }
  });