Edittext - 作为屏幕大小函数的字符数限制。可能吗?

时间:2014-12-04 18:50:56

标签: java android xml

我想对Edittext中的字符数有不同的限制。该限制必须取决于屏幕尺寸。有可能这样做吗?如果是,请分享您的想法。

2 个答案:

答案 0 :(得分: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)});

答案 1 :(得分: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;
    }
}