问题描述
我有EditText,用于在消息(电子邮件,短信)中输入内容.我希望消息立即在ActionDone按钮单击上发布.我为此使用以下代码:
I have EditText which is used for entering contents on messages (emails, sms). I want message to be immediately posted on ActionDone button click. I use following code for this:
message.setOnEditorActionListener((textView, i, keyEvent) -> {
switch (i) {
case EditorInfo.IME_ACTION_DONE:
if (messageCanBePosted()) {
SoftKeyboard.hide(message);
postMessage();
return true;
} else {
return false;
}
default:
return false;
}
});
但是我也希望此消息字段是多行的,就像在任何其他Messenger应用程序中一样.我可以通过以下代码来实现:
But also I want this message field to be multiline, like in any other messenger apps. I can achieve it with this line:
android:inputType="textMultiLine"
问题在于,添加此行后,ActionDone按钮开始像Enter按钮一样工作.因此,从未调用用于捕获EditorInfo.IME_ACTION_DONE
的回调.因此,每当用户按下该按钮时,光标就会移至新行,而不是发布消息.
The problem is that after adding this line ActionDone button starts acting like Enter button. So my callback for catching EditorInfo.IME_ACTION_DONE
is never called. So each time user press that button cursor moves to new line instead of posting message.
如何保持EditText的多行行为(可以在多行上显示文本)和ActionDone按钮?
How can I keep both multiline behavior of EditText (ability to show text on multiple lines) and ActionDone button?
推荐答案
最后,在这里搜索相似的线程后,我找到了解决方案.只需在您的活动/片段上添加以下行:
Finally, after searching here for similar threads I have found solution. Just need to add these lines on your Activity/Fragment:
editText.setHorizontallyScrolling(false);
editText.setMaxLines(Integer.MAX_VALUE);
由于某种原因,如果您从xml应用完全相同的设置,则它将不起作用.您应该以编程方式进行.
For some reason it doesn't work if you apply exact same setting from xml. You should do it programmatically.
还有另一种可能的解决方案-从EditText派生并手动应用EditorInfo.IME_ACTION_DONE
.但是对我来说,第一个解决方案看起来更简单.
There is also another possible solution - derive from EditText and apply EditorInfo.IME_ACTION_DONE
manually. But for me first solution looks simpler.
这篇关于如何使用ActionDone按钮(没有Enter按钮)实现多行EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!