android在edittext弄一个文本完全固定的行
it1352
帮助4人
问题说明
我想让 use 只输入 5 行,我试过了
I want to allow use to enter just 5 lines, I tried this
<EditText
android:layout_below="@ id/tv_signup_descriptionError"
android:
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
正确答案
#1
你不能使用任何 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;
}
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanghfici
系列文章
更多
同类精品
更多
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20