android如何在edittext中制作文本完全固定的行

android how to make text in an edittext exactly fixed lines(android如何在edittext中制作文本完全固定的行)
本文介绍了android如何在edittext中制作文本完全固定的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让 use 只输入 5 行,我试过了

I want to allow use to enter just 5 lines, I tried this

<EditText
    android:layout_below="@+id/tv_signup_descriptionError"
    android:id="@+id/et_signup_description"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dip"
    android:hint="@string/et_hint_enterDescription"
    android:singleLine="false"
    android:lines="5"
    android:gravity="top"
    android:scrollHorizontally="false"
    android:inputType="textMultiLine" 
    android:maxLines="5"/>

但我仍然可以在第五行之后按 Enter

but still I can press Enter after the fifth line

推荐答案

你不能使用任何 XML 属性来做到这一点.

You cannot do that using any XML attributes.

maxlines代表EditText的最大高度不是输入行数.

maxlines represents the maximum height of the EditText and not the number of input lines.

但是,您可以实现自己的代码来检查行数.

You can however implement your own code to check for the number of lines.

以下是不是我自己的代码,而是取自 这个答案.

mEditText.setOnKeyListener(new View.OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {

            // if enter is pressed start calculating
            if (keyCode == KeyEvent.KEYCODE_ENTER
                    && event.getAction() == KeyEvent.ACTION_UP) {

                // get EditText text
                String text = ((EditText) v).getText().toString();

                // find how many rows it cointains
                editTextRowCount = text.split("\n").length;

                // user has input more than limited - lets do something
                // about that
                if (editTextRowCount >= 7) {

                    // find the last break
                    int lastBreakIndex = text.lastIndexOf("
");

                    // compose new text
                    String newText = text.substring(0, lastBreakIndex);

                    // add new text - delete old one and append new one
                    // (append because I want the cursor to be at the end)
                    ((EditText) v).setText("");
                    ((EditText) v).append(newText);

                }
            }

            return false;
        }

这篇关于android如何在edittext中制作文本完全固定的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)