自动将冒号添加到Edittext

时间:2015-10-28 09:17:01

标签: android android-edittext

我想通过EditText mac地址添加到我的数据库中。

是否可以在每隔一个字符后添加冒号(:)?

冒号应直接显示在EditText

中 编辑:尝试过。而且我认为我是在正确的方式(你的答案证实了这一点:P)



        inputMac = (EditText) view.findViewById(R.id.editText_mac);
        inputMac.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) {
               if (s.length() == 2 || s.length() == 5 || s.length() == 7 || s.length() == 9 || s.length() == 12 ){
                    inputMac.setText(inputMac.getText() + ":");
               }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });




但现在在12个字符之后,我得到了123456789123 :::::

3 个答案:

答案 0 :(得分:1)

我已经回答了similar question,所以这就是你如何实现它的目的:

    String mTextValue;
    Character mLastChar = '\0'; // init with empty character
    int mKeyDel;


    myEditText.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

           boolean flag = true;
           String eachBlock[] = myEditText.getText().toString().split(":");
           for (int i = 0; i < eachBlock.length; i++) {
                if (eachBlock[i].length() > 6) {
                    flag = false;
                }
           }
           if (flag) {

              myEditText.setOnKeyListener(new View.OnKeyListener() {

               @Override
               public boolean onKey(View v, int keyCode, KeyEvent event) {

                      if (keyCode == KeyEvent.KEYCODE_DEL)
                            mKeyDel = 1;
                            return false;
                      }
                });

               if (mKeyDel == 0) {

                  if (((myEditText.getText().length() + 1) % 3) == 0) {
                      myEditText.setText(myEditText.getText() + ":");
                      myEditText.setSelection(myEditText.getText().length());
                  }
                  mTextValue = myEditText.getText().toString();
               } else {
                  mTextValue = myEditText.getText().toString();
                  if (mLastChar.equals(':')) {
                       mTextValue = mTextValue.substring(0, mTextValue.length() - 1);
                       myEditText.setText(mTextValue);
                       myEditText.setSelection(mTextValue.length());
                  }
                  mKeyDel = 0;
               }

          } else {
              myEditText.setText(mTextValue);
          }

       }

       @Override
       public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            if (s.length()>0) {// save the last char value
                mLastChar = s.charAt(s.length() - 1);
            } else {
                mLastChar = '\0';
            }
       }

       @Override
       public void afterTextChanged(Editable s) {}

  });

PS:它还处理删除字符。

答案 1 :(得分:0)

试试这个,

editText1.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
                *****APPLY YOUR LOGIC HERE*****
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }
        });
    }

答案 2 :(得分:0)

我试过,我想我找到了一种不那么复杂的方式。 (它不完美,但我想我会成功)

&#13;
&#13;
       inputMac.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                macLengthBefore = inputMac.length();
                Log.d("Textlänge BEFORE", macLengthBefore.toString());
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                macLengthAfter = inputMac.length();
                Log.d("Textlänge AFTER", macLengthAfter.toString());

                if (macLengthAfter > macLengthBefore && ((inputMac.getText().length() + 1) % 3 == 0) && inputMac.length() <= 15) {
                    inputMac.setText(inputMac.getText() + ":");
                    inputMac.setSelection(inputMac.getText().length());
                }

            }
        });
&#13;
&#13;
&#13;

感谢@Rami提供模数查询