Android将EditText限制为3行

时间:2016-12-07 19:41:09

标签: android android-layout textwatcher

我们有两个edittext小部件,一个限制为1行,另一个我们试图将其限制为3行。下面发布的XML代码适用于限制为1行,限制为3行失败。我有一个不太优雅的解决方案只需使用3个单行edittext小部件。我的所有研究都指出使用关键的听众或文本观察者进一步讨论键盘类型。无论哪种方式,我的能力水平都不确定如何编写或将这些代码放在哪里以获得这些支持的解决方案。我们想知道如何解决这个maxLines = 3输入问题。 最佳链接HERE

    <EditText
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:ems="10"
    android:id="@+id/etSecAnswer"
    android:layout_marginStart="140dp"
    android:layout_marginTop="230dp"
    android:textStyle="bold"
    android:textSize="16sp"
    android:maxLines="1"
    android:inputType="text"
    android:textColor="@color/color_Black"/>

<EditText
    android:layout_width="420dp"
    android:layout_height="wrap_content"
    android:ems="10"
    android:id="@+id/etNotes"
    android:layout_marginTop="260dp"
    android:layout_marginLeft="140dp"
    android:textStyle="bold"
    android:textSize="16sp"
    android:textColor="@color/color_Black"
    android:maxLines="3"
    android:inputType="text|textMultiLine" />

4 个答案:

答案 0 :(得分:2)

下面是getLineCount的一些小代码,但真正的问题是你有计数后要做什么?我不是它可以看到的可编辑属性的粉丝所以说这里做的是我发现LOCK的一个小概念并解锁EditText尝试一下

锁定EditText

    etNotes.setInputType(InputType.TYPE_NULL);
    etNotes.setFocusable(false);

取消锁定EditText

    etNotes.setInputType(InputType.TYPE_CLASS_TEXT);
    etNotes.setFocusableInTouchMode(true);
    etNotes.setFocusable(true);
    etNotes.requestFocus();

在onCreate中定义监听器

    int L;
private void addTextChangedListener() {
    etNotes.addTextChangedListener(new TextWatcher() {

        public void afterTextChanged(Editable s) {
        }

        public void beforeTextChanged(CharSequence q, int s, int c, int a) {
        }

        public void onTextChanged(CharSequence q, int s, int b, int c) {
            Log.d("TESTING", " LINES = " + etNotes.getLineCount());
            System.out.println("Line Count "+etNotes.getLineCount());

            L = etNotes.getLineCount();
            if(L > 3){
                etNotes.getText().delete(etNotes.getSelectionEnd() - 1,etNotes.getSelectionStart());
                //etNotes.append("\b");
                //L = 0;
                etNotes.setEnabled(false);

            }
        }
    });
}

让我们知道这是如何工作的,以便其他人可以从已发布的答案中获益9000帖子回过头而且目前的帖子就这样我只看到大约15个有合理答案我很高兴看到下面的网站扩展在这个问题上 CodePath Android Cliffnotes

答案 1 :(得分:0)

是的,遗憾的是,maxLines实际上对应于EditText的外部高度(可见多少行),而不是字段内的实际文本(限制实际键入的文本)。

然而,您可以获得键入的行数和//do something,例如删除最后一行而不必编写/处理三个单独的EditTexts

得到这样的计数:

if(yourText.getLayout().getLineCount() > lineLimit;){
    //do something like delete last
}

答案 2 :(得分:0)

尝试以下代码

android:lines="3"

答案 3 :(得分:0)

下面的代码段可以帮助您。尽管它在Kotlin中,但是您也可以在Java代码中考虑类似的内容。

// we are checking for line count we need to keep and according to that we are defining inputType dynamically.
val lineCount = placeHolder.numberOfLines
if (lineCount > 1) {
    edtCaption.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_MULTI_LINE
} else {
    edtCaption.inputType = InputType.TYPE_CLASS_TEXT
}

// we are setting flag of  single line false.
edtCaption.setSingleLine(false)

// here, we are deleting the text if it exceeds the max lines we defined.
edtCaption.addTextChangedListener(object : TextWatcher {
    override fun beforeTextChanged(charSequence: CharSequence, i: Int, i2: Int, i3: Int) {}
    override fun onTextChanged(charSequence: CharSequence, i: Int, i2: Int, i3: Int) {}

    override fun afterTextChanged(editable: Editable) {
        if (null != edtCaption.layout && edtCaption.layout.lineCount > lineCount) {
            edtCaption.text.delete(edtCaption.text.length - 1, edtCaption.text.length)
        }
    }
})

最终,它将使我们能够限制用户输入我们认为正确的行。