怎样的android使文字在一个EditText完全固定线路 [英] android how to make text in an edittext exactly fixed lines

查看:169
本文介绍了怎样的android使文字在一个EditText完全固定线路的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想允许使用进入只有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"
        />

但我依然可以preSS 输入第五行后

推荐答案

您不能使用任何XML属性做。

You cannot do that using any XML attributes.

的maxlines 重presents的最大高度的EditText上的和不可以的输入线的数目。

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

不过,您可以实现自己的code检查的行数。

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

以下是不是我自己的code ,但摘自<一个href="http://stackoverflow.com/questions/7092961/edittext-maxlines-not-working-user-can-still-input-more-lines-than-set/7103931">this回答。

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("\n");

                    // 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完全固定线路的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆