2009-05-26 2 views

risposta

2

È possibile implementare un TextChangedListener dove hai la certezza quelle parti del testo non otterrete deleted/overwritten.

class TextChangedListener implements TextWatcher { 
    public void afterTextChanged(Editable s) { 
       makeSureNothingIsDeleted(); 
    } 

    public void beforeTextChanged(CharSequence s, int start, int count, int after) {} 

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

TextChangedListener tcl = new TextChangedListener(); 
    my_editable.addTextChangedListener(tcl); 
16

Si potrebbe utilizzare

editText.setFocusable(false);

o

editText.setEnabled(false);

anche se la disattivazione del EditText non al momento ignora input dalla tastiera su schermo (credo che sia un insetto).

A seconda dell'applicazione potrebbe essere meglio usare un InputFilter che rifiuta tutte le modifiche:

editText.setFilters(new InputFilter[] { 
    new InputFilter() { 
     public CharSequence filter(CharSequence src, int start, 
      int end, Spanned dst, int dstart, int dend) { 
      return src.length() < 1 ? dst.subSequence(dstart, dend) : ""; 
     } 
    } 
});

vedere anche this domanda.

+2

Per mostrare/nascondere i campi password, il contenuto del testo viene cancellato se si utilizza questo trucco e quindi modificare il tipo di input ('editText.setInputType (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD)'). La soluzione alternativa consiste nell'impostare temporaneamente i filtri sull'array vuoto mentre si modifica il tipo di input. –

+0

@ David thx, buono a sapersi –

+1

@Josef +1 per buona risposta –