这是我在自己的项目中实现了 EditText 多行输入且 将回车键显示成“发送”
要实现多行且有“发送” 按钮的效果,一定要有下面的代码 1+2 缺一不可,不然会掉坑里的
代码1:
<EditText
android:id="@+id/etMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:imeOptions="actionSend" />
代码2:
etMessage?.maxLines = 4;
etMessage?.setHorizontallyScrolling(false);
etMessage?.setOnEditorActionListener { _, actionId, ketEvent ->
if (actionId == EditorInfo.IME_ACTION_UNSPECIFIED ||
actionId == EditorInfo.IME_ACTION_SEND ||
(ketEvent != null && ketEvent.keyCode == KeyEvent.KEYCODE_ENTER)
){
//执行发送逻辑
sendText()
true
}
false
}
必坑小结:
我试过在xml 页面 设置maxLines 但无法实现效果
还是要用java/kotlin代码设置 maxLines 才能实现效果
<EditText
android:id="@+id/etMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:maxLines = "4"
android:imeOptions="actionSend" />