2014-12-04 54 views
0

我想對Edittext中的字符數量有不同的限制。該限制必須取決於屏幕尺寸。是否有可能這樣做?如果是,請分享你的想法。Edittext - 作爲屏幕大小的函數的字符數限制。可能嗎?

+1

您可以這樣做。請發佈您嘗試的代碼,並告訴我們您遇到問題的位置。您的問題是否獲得屏幕尺寸?或者是限制「EditText」字符的問題? – 2014-12-04 18:55:10

+0

我的問題是獲取屏幕大小。我還沒有寫下代碼。我在開始之前正在考慮。如果我不發佈代碼,我希望它沒關係。雖然,我會確保我將編輯帖子,一旦我成功實現它,或者在編寫代碼時遇到同樣的問題。 – Slay 2014-12-04 18:57:23

+0

顯示myscreen = getWindowManager()。getDefaultDisplay(); int screenWidth = myscreen.getWidth(); 現在使用寬度限制您的編輯文本。 – vembutech 2014-12-04 18:58:40

回答

0

當然。你可以做的是在編輯文本中添加一個InputFilter。然後,您可以監視編輯文本文本寬度的更改。如果它超出了某一點,你可以砍掉額外的東西。在這種情況下,您希望將文本寬度限制爲可用於EditText的寬度。

您的輸入濾波器:

public class TextSizeFilter implements InputFilter { 
    Paint p; 
    int maxWidth; 

    /** 
    * A filter based on the maxWidth of the text. 
    * @param p Paint used by the View 
    * @param maxWidth Max width of the text 
    */ 
    public TextSizeFilter(Paint p, int maxWidth) { 
     this.p = p; 
     this.maxWidth = maxWidth; 
    } 

    @Override 
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { 
     float originalW = p.measureText(dest, 0, dstart); 
     float spaceLeft = maxWidth - originalW; 

     if (spaceLeft > 0) { 
      int w = p.breakText(source, start, end, true, spaceLeft, null); 

      if (w != source.length()) 
       return source.subSequence(0, start + w); 
     } else { 
      return ""; 
     } 

     return null; 
    } 
} 

然後,你需要衡量編輯文本的寬度(覈減由於不能使用的空間填充),並設置編輯文本的輸入濾波器。

EditText t = ... 
int maxWidth = t.getWidth() - t.getPaddingLeft() - t.getPaddingRight(); 
t.setFilters(new InputFilter[]{new TextSizeFilter(t.getPaint(), maxWidth)}); 
0

基於@ idunnololz的答案,我做了一些改變,因爲該版本似乎並沒有很好地工作,如果光標不是在文本的末尾,也不是佔改寫文本選擇。 我還添加了可選的最大數量的字符(所以文本不會超過視圖大小,也不會超過有限的字符數量)。

public class TextLengthFilter implements InputFilter { 

    private final static int NO_MAX_CHARACTERS = -1; 

    private Paint p; 
    private int maxWidth; 
    private int maxCharacters; 

    /** 
    * A filter based on the maxWidth of the text. 
    * @param p Paint used by the View 
    * @param maxWidth Max width of the text (in pixels) 
    */ 
    public TextLengthFilter(Paint p, int maxWidth) { 
     this(p, maxWidth, NO_MAX_CHARACTERS); 
    } 

    /** 
    * A filter based on the maxWidth of the text. 
    * @param p Paint used by the View 
    * @param maxWidth Max width of the text (in pixels) 
    * @param maxCharacters Max amount of characters for the text 
    */ 
    public TextLengthFilter(Paint p, int maxWidth, int maxCharacters) { 
     this.p = p; 
     this.maxWidth = maxWidth; 
     this.maxCharacters = maxCharacters; 
    } 

    @Override 
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { 
     float originalW = p.measureText(dest, 0, dest.length()); 
     //Calculate the amount of space that is being reclaimed by characters that are replaced 
     int toBeReplacedWidth = p.breakText(dest, dstart, dend, true, originalW, null); 
     float spaceLeft = (maxWidth - originalW) + toBeReplacedWidth; 
     int selectionLength = (dend - dstart); //The amount of characters that are going to be replaced 
     int changeLength = (end - start); 

     //Check if there are more characters after the change than before. 
     if (maxCharacters != NO_MAX_CHARACTERS && changeLength > selectionLength) { 
      //If the length of the original text was already too many characters don't allow more to be added. 
      if (dest.length() > maxCharacters) { 
       return ""; 
      } 
      int finalLength = dest.length() + changeLength - selectionLength; 
      //Check if the final length, after the replacement, doesn't exceed the maximum characters 
      if (finalLength > maxCharacters) { 
       //if it does limit the characters to be added to not exceed the maximum 
       end = end - (finalLength - maxCharacters); 
      } 
     } 
     //Check if the size of the characters does not exceed the maximum view width 
     if (spaceLeft > 0) { 
      int w = p.breakText(source, start, end, true, spaceLeft, null) ; 
      //If not all characters would fit only allow the ones that do fit 
      if (w != source.length()) 
       return source.subSequence(0, start + w); 
     } 
     else { 
      return ""; 
     } 

     return null; 
    } 
} 
相關問題