2016-03-03 85 views
12

我想在用戶輸入一個字符時自定義一個編輯文本,然後編輯文本將其更改爲圖像。 看起來像圖像:enter image description here將Edittext輸入字符自定義爲圖像?

注意:●是一個圖像不符號。 EditText上的inputType在你的XML

+0

你可以在用戶輸入內容時用*替換char。保存用戶使用EditorActionListener輸入的文本,然後用*替換。..不確定圖片的位置 – Dhina

+0

向我們展示您到目前爲止所嘗試的內容。 – Mangesh

+0

現在我還沒有做任何事情。因爲我沒有經驗。 –

回答

20

您需要擴展PasswordTransformationMethod並使用方法EditText

edt.setTransformationMethod(new CustomPasswordTransformationMethod()); 

並粘貼此CustomPasswordTransformationMethod

class CustomPasswordTransformationMethod extends PasswordTransformationMethod { 
    @Override 
    public CharSequence getTransformation(CharSequence source, View view) { 
     return new PasswordCharSequence(source); 
    } 

    private class PasswordCharSequence implements CharSequence { 
     private CharSequence source; 
     public PasswordCharSequence(CharSequence source) { 
      this.source = source; 
     } 
     public char charAt(int index) { 
      if(index>4) //your own condition, when you want to hide characters. 
       return 0x2022; // change this to bullets you want like '*' or '.' 
      return source.charAt(index); 
     } 
     public int length() { 
      return source.length(); 
     } 
     public CharSequence subSequence(int start, int end) { 
      return source.subSequence(start, end); 
     } 
    } 
} 

上面的代碼將寫入字符,因爲它是高達5個字符,之後,將在EditText打印子彈。從this採取

基準柱

UPDATE

最後這裏是你的答案:

Spannable.Factory spannableFactory; 
int lastIndex = -1; 

spannableFactory = Spannable.Factory 
      .getInstance(); 

1.在您的EditText添加addTextChangedListener

mEditText.addTextChangedListener(watcher); 

    TextWatcher watcher = 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 (start>4) { 
       mEditText.removeTextChangedListener(watcher); 
       mEditText.setText(getIconText(context, s, start)); 
       mEditText.addTextChangedListener(watcher); 
       mEditText.setSelection(s.length()); 
      } 
     } 

     @Override 
     public void afterTextChanged(Editable s) { 

     } 
    }; 
  • 轉換您可繪製成Spannable

    public Spannable getIconText(Context context, CharSequence text, int index) { 
        Spannable spannable = spannableFactory.newSpannable(text); 
        if (index>lastIndex) { 
         spannable.setSpan(new ImageSpan(context, R.drawable.bullet_point), 
          index, index + 1, 
          Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
        } 
        lastIndex=index; 
        return spannable; 
    } 
    
  • enter image description here

    +0

    是的,謝謝。但我不想把它改爲子彈或'*',我想改變形象。 –

    +0

    啊謝謝。我現在會嘗試。 –

    +2

    好的。它完美地工作。謝謝大大。 –

    2

    使用屬性

    android:inputType="textPassword" 
    
    +0

    android:inputType =「textPassword」將所有字符改爲掩碼 但我想要某些字符不是掩碼,其他字符都是我的圖片掩碼。 –

    +2

    @HhchChunleng在這種情況下,你需要自定義PasswordTransformationMethod製作自定義類,它擴展了PasswordTransformationMethod並實現你的邏輯 – Ajinkya

    4

    我們,如果你要替換的字符b與性格說。然後你就可以添加TextWatcher這一點,它看起來像這樣

    myEditText.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) { 
    
          StringBuilder myText = new StringBuilder(myEditText.getText().toString()); 
          if (myText.toString().contains("b")){ //If this contains b 
           myText.setCharAt(myText.indexOf("b"),'●'); 
           myEditText.setText(myText.toString()); //Sets the string to EditText 
           myEditText.setSelection(myText.length()); //Moves cursor to the last after replacing 
          } 
         } 
    
         @Override 
         public void afterTextChanged(Editable s) { 
    
         } 
        }); 
    

    UPDATE

    我們可以使用setCompoundDrawablesWithIntrinsicBounds放置的圖像,但不能把它作爲在準確位置的字符。我們只能定義邊界。

    +1

    他必須改變它與圖像沒有任何字符,你有任何想法做到這一點? –

    +1

    @AnujSharma我們可以使用'setCompoundDrawablesWithIntrinsicBounds',但不能將它作爲一個字符放在確切的位置。我們只能定義邊界。 –

    2

    在代碼嘗試此。 doc

    EditText text = (EditText) findViewById(R.id.edtTest); 
        text.setCompoundDrawablesWithIntrinsicBounds(null, null, 
             getResources().getDrawable(R.drawable.myDrawable), null); 
    
    +0

    是的,我會嘗試。 –