我有用于在消息(电子邮件、短信)上输入内容的 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;
}
});
但我也希望这个消息字段是多行的,就像在任何其他信使应用程序中一样.我可以用这条线来实现它:
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?
最后,在这里搜索类似的线程后,我找到了解决方案.只需在您的 Activity/Fragment 上添加这些行:
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!