0

谁能帮我解决这个问题。?我在电话号码之间添加了破折号(-)。下面我提到了我的代码。

// 从这里移除

现在我想在按下后退按钮时删除破折号(-)。就我而言,我无法做到这一点。假设如果我插入了 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
                }
            });
        }
    }
4

1 回答 1

1

按照这个方法你的问题就解决了

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

   }
  });
于 2013-09-18T10:53:18.030 回答