2013-04-16 70 views
0

自定義editext在android系統

輸入我如何自定義在給定的figure.Basically我的要求顯示editext的輸入類型是EditText上應該顯示只有最後3位或4位只有初始的12位應在密碼模式。

+3

你必須這樣做編程,我的意思是,有沒有這種情況下的XML配置,你有嘗試過嗎? – Daniel

+1

只需將觀察者添加到editText中,並在達到所需長度時以編程方式更改輸入類型。 – Daniel

回答

0

您需要添加一個TextWatcherEditText

int characterCount = 0; 
int asteriskCount = 0; 
CharSequence input = null; 
input.addTextChangedListener(new TextWatcher() { 
    @Override 
    public void onTextChanged(CharSequence s, int start, int before, int count) { 
     characterCount = count; 
     //update input sequence based on changes. 
    } 

    @Override 
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
     //update input sequence based on changes. 
    } 

    @Override 
    public void afterTextChanged(Editable s) { 
     if (asteriskCount != characterCount) { 
      //make the visible sequence here. 
      CharSequence seq = ""; 
      for (int i = 0; i < (characterCount <= 12 ? characterCount : 12); i++) { 
       seq = seq + "*"; 
      } 
      if (characterCount > 12) { 

       for (int i = 12; i < characterCount; i++) { 
        seq = seq + characterCount.charAt(i); 
       } 
      } 
      asteriskCount = characterCount; 
      input.setText(seq); 
     } 
    } 
}); 
0

沒有像這樣的內置功能。所以你必須自己做。更改文字時,您必須對文字進行更改。這樣做。

如果通過擴展EditText來創建自定義editText,則可以覆蓋onTextChanged方法並處理更改。

或者您可以使用TextWatcher進行更改。

所以當文本改變時,除了最後3位數字之外的數據都設置爲*。

但請記住,您必須使用字符串字段將原始數據存儲在字段中。

+0

這個答案應該是一個評論。 – Daniel

+0

感謝Daniel和StinePike爲您的投入..我會嘗試併發布我的解決方案 – Amritesh

-2

下面是我TextWatcher的代碼片段:

 private boolean spaceDeleted; 
     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      characterCount = start; 
      //update input sequence based on changes. 

     } 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 
      // check if a space was deleted 
      CharSequence charDeleted = s.subSequence(start, start + count); 
      spaceDeleted = " ".equals(charDeleted.toString()); 

     } 

     @Override 
     public void afterTextChanged(Editable s) { 
      if(s.length()>12){ 
       return; 
       } 
      System.out.println("Character Count in afterTextChange->"+characterCount); 
      System.out.println("Editable Character->"+s); 
      ccNumber.removeTextChangedListener(this); 
      // record cursor position as setting the text in the textview 
      // places the cursor at the end 
      int cursorPosition = ccNumber.getSelectionStart(); 
      String withSpaces = formatText(s); 
      ccNumber.setText(withSpaces); 
      // set the cursor at the last position + the spaces added since the 
      // space are always added before the cursor 
      ccNumber.setSelection(cursorPosition + (withSpaces.length() - s.length())); 

      // if a space was deleted also deleted just move the cursor 
      // before the space 
      if (spaceDeleted) { 
       ccNumber.setSelection(ccNumber.getSelectionStart() - 1); 
       spaceDeleted = false; 
      } 

      // enable text watcher 
      ccNumber.addTextChangedListener(this); 



     } 

     private String formatText(CharSequence s) { 
      // TODO Auto-generated method stub 

      StringBuilder formatted = new StringBuilder(); 
      int count = 0; 
      /* if(s.length()<12){ 
       formatted.append("*"); 
      }else{ 
       formatted.append(s.charAt(characterCount)); 
      }*/ 
      for (int i = 0; i < s.length(); ++i) 
      { 

       formatted.append("*"); 

       /*if (Character.isDigit(s.charAt(i))) 
       { 
        if (count % 4 == 0 && count > 0) 
         formatted.append(" "); 
        formatted.append(s.charAt(i)); 
        ++count; 
       }*/ 
      } 
      return formatted.toString(); 
     } 
    }); 
+0

@shoerat答案確實對我有幫助,但這不是完整的答案,它向我展示了正確的方向。所以想到把我的答案 – Amritesh

+0

我嘗試與菲爾代碼,但這是不正確的,並與這些代碼snipptes問題 – Amritesh

+0

是啊?你可以建議對Phil的代碼進行修正,然後...... – ozbek