首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓编辑- maxLength

安卓编辑- maxLength
EN

Stack Overflow用户
提问于 2014-08-01 09:25:26
回答 3查看 2.5K关注 0票数 2

我知道可以在EditText中设置输入文本的最大长度,方法是

代码语言:javascript
复制
android:maxLength="10".

但是,我是否也可以设置最大长度,以便一旦整个EditText被文本填充,用户就不能再输入任何内容了?

就像这样:

代码语言:javascript
复制
|_________________________|   <-- empty EditText   

|texttexttexttext_________|   <-- user can still type in text

|texttexttexttexttexttextt|  <-- that's it, user can't type in stuff anymore

我怎么能做到这一点?

编辑:,换句话说:我不希望它被限制为10个字符,但是我想限制它,这样一旦整个EditText被填充(这取决于屏幕大小),用户就不能再输入内容了。

EN

回答 3

Stack Overflow用户

发布于 2014-08-01 09:29:30

嗯,我认为这可能是一种方法:

  1. 获取EditText宽度
  2. 获取文本宽度

在任何情况下,这里的代码都不是完美的,它允许用户放置比宽度稍长的地方(可能是因为EditText.getWidth()会返回带边框的值等等)。只是想给你个主意。

这种方法的更多问题在于,即使我们没有在EditText中显示文本,键盘缓冲区仍然会知道这一点,并且会给用户造成不好的影响,我试着使用InputFilter,但是结果是一样的(嗯,它更容易出错)。

代码语言:javascript
复制
private int widthEditText;

// [...] in onCreate:

final EditText editText = (EditText) findViewById(R.id.text);
// p.s in onCreate the layout is not created yet, so i should wait to read the width
editText.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        widthEditText = editText.getWidth();
    }
});

editText.addTextChangedListener(new TextWatcher() {
    private String previousText;

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        Paint paint = editText.getPaint();
        Rect rectangle = new Rect();

        /* i used getTextBounds instead of measureText because it will return the float value but getWidth of
         * edit text is a int. */
        paint.getTextBounds(editable.toString(), 0, editable.length(), rectangle);
        Log.d(TAG, "Text width: " + rectangle.width());

        if (rectangle.width() > widthEditText) {
            /* the user cannot type anymore */
            Log.d(TAG, widthEditText + " > " + rectangle.width() + " blocked.");
            editable.replace(0, editable.length(), previousText);
            return;
        }

        previousText = editable.toString();
        Log.d(TAG, "Continue to write, new previousText: "  + previousText);
    }
});

为了看一看,我尝试使用measureText,它返回浮动中的宽度(并对int和float进行比较),结果是相同的:

代码语言:javascript
复制
@Override
public void afterTextChanged(Editable editable) {
    Paint paint = editText.getPaint();
//                Rect rectangle = new Rect();

    /* i used getTextBounds instead of measureText because it will return the float value but getWidth of
     * edit text is a int. */
//                paint.getTextBounds(editable.toString(), 0, editable.length(), rectangle);
    float width = paint.measureText(editable.toString());

    Log.d(TAG, "Text width: " + width);

    if (width > widthEditText) {
        /* the user cannot type anymore */
        Log.d(TAG, widthEditText + " > " + width + " blocked.");
        editable.replace(0, editable.length(), previousText);
        return;
    }

    previousText = editable.toString();
    Log.d(TAG, "Continue to write, new previousText: "  + previousText);
}

结果完全一样..。

我和android:ems="4"玩过,它应该提供字体大小(也许可以做些什么来使它更适合?)但是没有什么(不是,看起来更好的东西,但我觉得它完全是随机的,基于这些话)。

就我个人而言,我会避免这样做,因为对我来说,这听起来有点随意.也许还有更好的方法我不知道..。

我用三星的S4设备测试了它,所以我不知道它是否适用于其他设备!抱歉,但是Genymotion不起作用

票数 0
EN

Stack Overflow用户

发布于 2014-08-01 09:36:27

使用这个

代码语言:javascript
复制
yourEditText.addTextChangedListener(new TextWatcher(){

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }
        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub

            if(yourEditText.getText().toString().length()>=10){
                yourEditText.setEnable(false)
            }


        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
票数 0
EN

Stack Overflow用户

发布于 2014-08-01 09:28:45

您可以使用android:singleLine=true

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25076999

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档