2015-10-28 71 views
1

我想通過EditTextMAC地址添加到我的數據庫。自動添加結腸EDITTEXT

是否有可能第二個字符後加冒號(:)?

結腸應直接在EditText顯示。

編輯:試過了。而且我覺得我是在正確的道路(你anwers證實了這一點: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 :::::

+0

你想知道到底是什麼?把':'放在'edittext'上? – Aspicas

+0

我想要一個'EditText'。每隔一個字符出現一個':'應該出現。 我什麼都沒試過,因爲我不知道如何在輸入過程中將一個字符添加到'Edittext'。 – Rastaman

+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:這也處理刪除字符。

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 

      } 
     }); 
    } 
+0

爲什麼我必須使用'onTextChanged'方法? atm我使用'afterTextChanged'來檢查每次更改後的輸入。 – Rastaman

+0

因爲您將在OnTextChanged中將EditText的文本顯示爲CharSequence。 – activesince93

0

我想我想我找到辦法至極一個並不複雜。 (它並不完美,但我想我會讓它)

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

 
      } 
 
     });

感謝@Rami用於模查詢