Android为EditText设置新行和长度限制

问题描述 投票:0回答:1

我试图通过限制新行和字符的数量来限制用户过度填充UI。

我正在使用下面的代码,但问题是getLineCount()似乎只获得\n计数而不是行计数。因此,如果没有\n字符的新行,用户仍然可以溢出。

因此,如果用户输入3行文本,然后他仍然可以添加5行新行,他可以搞砸他和其他UI。那么我该如何修复这个bug呢?

注意:我已经尝试过XML行限制属性,但它不起作用:android:maxLines="5"

     etDescription.addTextChangedListener(new TextWatcher()
                {

                    boolean _ignore = false; // indicates if the change was made by the TextWatcher itself.
                    final int DESCRIPTION_LETTER_LIMIT = 168;
                    final int DESCRIPTION_LINE_LIMIT = 5;

                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after)
                        {

                            if(after > DESCRIPTION_LETTER_LIMIT || etDescription.getLineCount() > DESCRIPTION_LINE_LIMIT )
                                {
                                    Log.d(TAG, "beforeTextChanged: limit reached. After count and line count: "+ after+ " | "+etDescription.getLineCount());
                                    isLimitReached = true;
                                }
                            else
                                {
                                    Log.d(TAG, "beforeTextChanged: limit NOT reached. After count and line count: "+ after+ " | "+etDescription.getLineCount());

                                    isLimitReached = false;
                                }
                        }


                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count)
                        {
                        }

                    @Override
                    public void afterTextChanged(Editable s)
                        {
                            if (_ignore)
                                return;

                            _ignore = true; // prevent infinite loop
                            // Change your text here.
                            if(isLimitReached)
                                {
                                    if(etDescription.getSelectionEnd() - 1 != -1)
                                        {

                                            etDescription.getText().delete(etDescription.getSelectionEnd() - 1, etDescription.getSelectionStart());
                                            isLimitReached = false;
                                        }
                                }

                            _ignore = false; // release, so the TextWatcher start to listen again.
                        }
                });
android user-interface android-edittext
1个回答
0
投票

试试这个吧

  <EditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingStart="8dp"
            android:paddingEnd="8dp"
            android:lines="1"/>

或者您也可以在代码中执行此操作

EditText editText = findViewById(R.id.editText);
editText.setLines(1);

你可以在doc here上阅读更多内容。

© www.soinside.com 2019 - 2024. All rights reserved.