带有自动换行的Android EditText但没有硬回车
如果我在EditText小部件上设置SingleLine = true,我会得到一个单行编辑控件,不允许用户插入硬返回(单击Enter移动到下一个字段而不是插入新行) 。如果我没有设置SingleLine = true,则用户可以插入硬回车。
If I set SingleLine=true on an EditText widget, I get a single-line edit control that doesn't allow hard returns to be inserted by the user (clicking Enter moves to the next field instead of inserting a new line). If I don't set SingleLine=true, the user can insert hard returns.
如果我设置layout_height =wrap_content,EditText控件将垂直增长以显示所有的文字。但是,只有当SingleLine没有设置为true时才会这样做。
If I set layout_height="wrap_content", the EditText control will grow vertically to show all of the text. However, it only does this if SingleLine is not set to true.
所以,我的问题是,是否可以在不允许的情况下进行自动换行和垂直调整大小用户输入硬换行符?我想我可以捕获输入按键,但是我还必须抓住其他方式,他们可能会在那里得到一个(复制/粘贴,不知道还有什么?)。有没有一种简单的方法可以通过正确的属性组合来实现这一点?
So, my question is, is it possible to get the word-wrapping and vertical resizing, without allowing the user to enter hard line breaks? I guess I could trap the enter keypress, but then I would also have to catch other ways they might get one in there (copy/paste, not sure what else?). Is there a simple way to do this with just the right combination of properties?
我更喜欢自动换行,用户可以看到所有文本,与单行编辑控件的水平滚动,但我真的不希望他们认为他们可以输入多行文本(我不想支持它)。我想我可能只是在将数据保存到数据库时将硬返回转换为空格,如果必须的话(我在PC上同步这些数据的遗留应用程序无法处理硬回车)。
I prefer the word-wrap where the user can see all of the text, compared to the horizontal scrolling of a single-line edit control, but I don't really want them thinking they can enter multiline text (and I don't want to have to support it). I guess I might just convert hard returns to spaces when I save the data to my database, if I have to (the legacy app I sync this data to on the PC can't handle hard returns).
我也在寻找能做到这一点的事情。我找到的唯一解决方案是扩展EditText,如下所示:
I too was looking for something to do this as well. The only solution I found was to extend the EditText as follows:
package com.kylemilligan.test;
import android.content.Context;
import android.util.AttributeSet;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.widget.EditText;
public class NoNewlineEditText extends EditText
{
public NoNewlineEditText(Context context) {
super(context);
}
public NoNewlineEditText(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
}
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs)
{
InputConnection connection = super.onCreateInputConnection(outAttrs);
int imeActions = outAttrs.imeOptions & EditorInfo.IME_MASK_ACTION;
if ((imeActions & EditorInfo.IME_ACTION_DONE) != 0)
{
// clear the existing action
outAttrs.imeOptions ^= imeActions;
// set the DONE action
outAttrs.imeOptions |= EditorInfo.IME_ACTION_DONE;
}
if ((outAttrs.imeOptions & EditorInfo.IME_FLAG_NO_ENTER_ACTION) != 0)
{
outAttrs.imeOptions &= ~EditorInfo.IME_FLAG_NO_ENTER_ACTION;
}
return connection;
}
}
然后在XML中使用如:
And then in XML use like:
<com.kylemilligan.test.NoNewlineEditText
android:id="@+id/noNewLineText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top|left"
android:imeOptions="actionDone"
android:minLines="5" />
希望这有帮助!