Rus*_*lan 18 android multiline ime android-edittext imeoptions
我有EditText,用于在消息(电子邮件,短信)上输入内容.我希望消息能够立即发布在ActionDone按钮上.我使用以下代码:
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;
}
});
Run Code Online (Sandbox Code Playgroud)
但我也希望这个消息字段是多行的,就像在任何其他信使应用程序中一样.我可以用这条线来实现它:
android:inputType="textMultiLine"
Run Code Online (Sandbox Code Playgroud)
问题是在添加此行后,ActionDone按钮开始像Enter按钮一样.所以我EditorInfo.IME_ACTION_DONE
从来没有调用过捕获的回调.因此,每次用户按下该按钮光标移动到新行而不是发布消息.
如何保持EditText的多行行为(在多行显示文本的能力)和ActionDone按钮?
Rat*_*i J 35
使用
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
Run Code Online (Sandbox Code Playgroud)
并在XML中:
android:inputType="textMultiLine"
Run Code Online (Sandbox Code Playgroud)
Rus*_*lan 32
最后,在这里搜索类似的线程后,我找到了解决方案.只需要在Activity/Fragment上添加以下行:
editText.setHorizontallyScrolling(false);
editText.setMaxLines(Integer.MAX_VALUE);
Run Code Online (Sandbox Code Playgroud)
出于某种原因,如果您从xml应用完全相同的设置,它将不起作用.你应该以编程方式进行.
还有另一种可能的解决方案 - 从EditText派生并EditorInfo.IME_ACTION_DONE
手动应用.但对我来说,首先解决方案看起来更简单
Nom*_*que 16
继续鲁斯兰的回答.这个技巧有效,但还有一件事你需要用XML来处理.
EditText
应该有输入类型text
否则actionDone
将无法工作.默认输入类型EditText
允许用户输入换行符,因此inputType应设置为text
ie
android:inputType="text"
//And of course
android:imeOptions="actionDone"
Run Code Online (Sandbox Code Playgroud)
在你的java类中,你需要添加:
editText.setHorizontallyScrolling(false);
Run Code Online (Sandbox Code Playgroud)