Android EditText设置Maxlines不生效\TextView设置Maxlines和ellipsize不生效

解决了什么问题?

1、EditText设置Maxlines无效,还是会折行

2、TextView设置Maxlines和ellipsize不生效

问题描述

我们在使用EditText的时候,如果用户输入太长就会折行,我们希望设置行数来限制用户的输入,于是设置maxlines参数为1,但是当用户输入超过1行时还会折行,现在就来看看原因和解决方法。

使用TextView希望只显示一行,然后超出的部分打点,设置Maxlines和ellipsize=“end”,TextView只显示了一行,但是没有打点。

Maxlines是怎么做的和singleLine有什么区别?

EditText是继承TextView实现的,所以我们先看看两个方法直接有什么区别。

android:maxLines Makes the TextView be at most this many lines tall.

android:singleLine Constrains the text to a single horizontally scrolling line instead of letting it wrap onto multiple lines, and advances focus instead of inserting a newline when you press the enter key.

可以看出,maxLines 是在限制高度, singleLine 是强制不让换行。具体效果有什么区别呢? 从高度来讲是一样的,两者肯定都显示一行,但从换行的位置来讲就有区别了,maxLines并不会改变其换行的位置,而singleLine则会。如果超过一行singleLine会在一行内显示,ellipsize为end后面加上"…",而maxlines=“1” 则不会,它依然会在原来换行的位置换行,所以有时候一行不满,但是却不显示剩下的部分。

解决方法及原理

1、EditText设置Maxlines无效,还是会折行
解决方法1:

通过以上的分析,显而易见,我们可以通过设置singleline为true就可以让EditText不折行。

解决方法2:

设置EditText的InputType。为什么设置inputType就会正常呢?我们需要看一段代码。

public void setInputType(int type) {
   ...
    boolean singleLine = !isMultilineInputType(type);

    // We need to update the single line mode if it has changed or we
    // were previously in password mode.
    if (mSingleLine != singleLine || forceUpdate) {
        // Change single line mode, but only change the transformation if
        // we are not in password mode.
        applySingleLine(singleLine, !isPassword, true);
    }
		...
 
}

当我们设置inputType的时候回调用applySingleLine方法

private void applySingleLine(boolean singleLine, boolean applyTransformation,
        boolean changeMaxLines) {
    mSingleLine = singleLine;
    if (singleLine) {
        setLines(1);
        setHorizontallyScrolling(true);
        if (applyTransformation) {
            setTransformationMethod(SingleLineTransformationMethod.getInstance());
        }
    } else {
        if (changeMaxLines) {
            setMaxLines(Integer.MAX_VALUE);
        }
        setHorizontallyScrolling(false);
        if (applyTransformation) {
            setTransformationMethod(null);
        }
    }
}

然后调用设置横向滚动等信息,最后调用setTransformationMethod方法

public final void setTransformationMethod(TransformationMethod method) {
    if (method == mTransformation) {
        // Avoid the setText() below if the transformation is
        // the same.
        return;
    }
    if (mTransformation != null) {
        if (mSpannable != null) {
            mSpannable.removeSpan(mTransformation);
        }
    }
    ...
    setText(mText);
 		...
}

mSpannable.removeSpan(mTransformation);这个方法移除了TextView的内部折行等信息,所以当我们设置了inputType的时候,也不会出现问题。

2、TextView设置Maxlines和ellipsize不生效
解决方法1:

通过以上的分析,显而易见,我们可以通过设置singleline为true就可以让TextView不折行,进而出现我们需要的打点效果。
需要注意的是,一般情况下设置Maxlines和ellipsize在渲染时是不会出现不生效的问题的。

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值