2011-07-25 22 views
3

我有一種情況,我希望用戶爲我完成一個句子。例如,考慮一個EditText帶有「上次我」的暗示。通常,當用戶點擊EditText時,提示消失,但我希望它留下。另外,我希望文本是永久性的,以便它不能被刪除......只讓用戶有一個選項......完成句子。在EditText中的永久提示

第一部分相當簡單,只需使用EditText的setText()方法來放置提示。困難的部分是後者。我怎樣才能在EditText文本中的用戶不能擦除?

回答

2

那麼你不能在代碼中做到這一點?有些算法喜歡,如果文本少於16個字符(「最後一次我」的長度),然後設置文本。因此,每當他們點擊它時,如果他們試圖擦除它,它就會回到默認文本。

此外,另一個想法..爲什麼不只是使一個TextView這右邊緣與EditText框的左邊緣對齊,用戶永遠不會知道它是另一個盒子。這是最好的解決方案,如果你不希望被編輯的文本,只是讓它成爲一個TextView

+0

我喜歡第一個想法。我正在嘗試它。 – dfetter88

+0

你已經指出了我正確的方向。我將實現'TextWatcher'接口,然後在'afterTextChanged'方法中查看是否第一個X字符是我想要的...如果不是,請更正它們。 – dfetter88

+1

好主意,只是想知道,爲什麼你不想把「最後一次我」放在TextView中? – Andrew

0

只是檢查長度是不夠的......我可以鍵入「這是一個真正的長長的文本我放入箱子「,它會接受它,即使它不是以」最後一次我「字符串開頭。

就我個人而言,我可能會採取建議使用TextView而不是檢查出路的預防方法。但是如果你之後要驗證它,你實際上需要檢查返回字符串的開頭。

+0

嗯......什麼?不,它不會。它會檢查你的字符串是否以「最後一次我」開頭。如果是的話,那就什麼都沒有。如果沒有,它會解決它。而且由於這個方法在每次textchange時都會被調用,所以事情不會像你的例子那樣失控。文本只能是指定區域中的一個字符或一個字符長。 – dfetter88

+0

我的檢查是這樣的:'if inputStr.startsWith(startString)'它不檢查長度。 – dfetter88

+0

這是正確的...... startsWith()完全按照我的建議 - 「檢查返回的字符串的開頭」。它不會做第一個答案的建議「有些算法喜歡,如果文本少於16個字符(」最後一次我「的長度),然後將文本設置爲。」 –

2

描述的問題可以使用android.text.TextWatcher來解決。

public class CompleteSentenceWathcher implements TextWatcher { 

    private final String initialText; 

    private int start; 
    private int after; 
    private int count; 

    public CompleteSentenceWathcher(String initialText) { 
     this.initialText = initialText; 
    } 


    @Override 
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
     this.start = start; 
     this.count = count; 
     this.after = after; 
    } 

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

    @Override 
    public void afterTextChanged(Editable s) { 
     if(start < initialText.length()) { 
      if(s.toString().startsWith(initialText)) { 
       return; 
      } 
      if(count >= 1 && after == 0) { 
       if(start+count+1 <= initialText.length()) { 
        s.replace(start, start+count, initialText.substring(start, start+count+1)); 
       } else { 
        s.replace(start, start, initialText.substring(start, start+1)); 
       } 
      } else if(count == 0 && after >= 1) { 
       s.delete(start, start+after); 
      } 
     } 
    } 

} 

創建一個EditText的實例並添加TextWatcher。

EditText editText = new EditText(this); 
     editText.setText("I love"); 
     editText.addTextChangedListener(new CompleteSentenceWathcher(editText.getText().toString())); 
2

我有InputFilter,其中_PERMANENT_HINT_TEXT是在EditText結束,我不希望用戶能夠修改文本來實現這一點。我建議給它添加一個顏色範圍,以便它變成灰色,希望看起來像文本的提示/禁用部分。這應該有希望改善用戶體驗,因爲他們應該自動認爲它是不可修改的,而不只是想知道爲什麼EditText(他們通常可以完全改變)的某些部分不是「工作」的。這種方法允許在 InputFilter設置在EditText後設置文本,這是我需要的,因爲我在EditTextPreference上使用了這個。

爲了清楚起見,我需要永久文本存在於EditText的末尾,而不是開頭,但這應該與我的實現對稱。

new InputFilter() { 
    @Override 
    public CharSequence filter(CharSequence source, int source_start, int source_end, 
           Spanned destination, int destination_start, int destination_end) { 
     final int protected_text_start = (TextUtils.isEmpty(destination)? source.length() : destination.length()) - _PERMANENT_HINT_TEXT.length(); 
     // Allows input into unprotected region 
     if (source_start + destination_start - source_end < protected_text_start) 
      return null; 
     // Prevents deletion of protected region 
     else if (TextUtils.isEmpty(source)) 
      return destination.subSequence(destination_start, destination_end); 
     // Ignores insertion into protected region 
     else 
      return ""; 
    } 
} 

使用EditText.setFilters(new InputFilters[] { /* InputFilter goes here */ };將其添加到所期望的EditText