是否可以同时设置 android:inputType="textMultiLine"
和 android:imeOptions="actionDone"
属性来创建一个多行文本框,并让键盘上的操作按钮显示“完成”而不是“回车”(Enter)?
我想要一个多行编辑框,并希望键盘上的操作按钮是“完成”,而不是“回车”(Carriage Return),但似乎无法实现。
是否可以同时设置 android:inputType="textMultiLine"
和 android:imeOptions="actionDone"
属性来创建一个多行文本框,并让键盘上的操作按钮显示“完成”而不是“回车”(Enter)?
我想要一个多行编辑框,并希望键盘上的操作按钮是“完成”,而不是“回车”(Carriage Return),但似乎无法实现。
使用
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
以及在XML中:
android:inputType="textMultiLine"
@BindingAdapter("rawInputType") fun setRawInputType(view: EditText, inputType: Int) { view.setRawInputType(inputType) }
。然后在布局XML的数据部分中:<import type="android.text.InputType" />
,并在EditText中使用app:rawInputType="@{InputType.TYPE_CLASS_TEXT}"
。 - ubuntudroid根据Android文档: '"textMultiLine" 正常的文本键盘,允许用户输入包含换行符(回车)的长文本字符串。' 因此,如果你想要在键盘上有 '完成' 按钮,则 textMultiLine 属性不适用。
一个简单的方法来获得带有 '完成' 按钮的多行(3行)输入字段是使用 EditText
android:lines="3"
android:scrollHorizontally="false"
然而,由于某些原因,只有我在代码中进行这些设置(在onCreate方法中)时才有效,而不是在布局文件中进行设置。
Translated:然而,由于某些原因,只有我在代码中进行这些设置(在onCreate方法中)时才有效,而不是在布局文件中进行设置。
TextView tv = (TextView)findViewById(R.id.editText);
if (tv != null) {
tv.setHorizontallyScrolling(false);
tv.setLines(3);
}
我希望这能帮助到某些人,因为花了相当长的时间才弄清楚。如果您找到了从清单中使其正常工作的方法,请告诉我们。
maxLines()
而不是setLines()
。 - Daniel SmithsetMaxLines(3)
)。非常感谢! - laurencevssetMaxLines()
并将 lines
设为 1。非常感谢! - Dooskington工作示例! 创建以下自定义EditText类以支持此功能,并在xml文件中使用该类。 工作代码:
package com.example;
import android.content.Context;
import android.util.AttributeSet;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.widget.EditText;
public class ActionEditText extends EditText
{
public ActionEditText(Context context)
{
super(context);
}
public ActionEditText(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public ActionEditText(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs)
{
InputConnection conn = super.onCreateInputConnection(outAttrs);
outAttrs.imeOptions &= ~EditorInfo.IME_FLAG_NO_ENTER_ACTION;
return conn;
}
}
<com.example.ActionEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:inputType="textAutoCorrect|textCapSentences|textMultiLine" />
要在 Kotlin 中执行此操作(并且还可以选择应用其他配置,例如 textCapSentences
),您可以使用此扩展函数:
// To use this, do NOT set inputType on the EditText in the layout
fun EditText.setMultiLineCapSentencesAndDoneAction() {
imeOptions = EditorInfo.IME_ACTION_DONE
setRawInputType(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES or InputType.TYPE_TEXT_FLAG_MULTI_LINE)
}
用法:
myEditText.setMultiLineCapSentencesAndDoneAction()
setRawInputType
而不是 setInputType
。 - Tamoxin对我来说,唯一有效的方法是在代码中设置这些值。
edittext.inputType = EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE
edittext.setHorizontallyScrolling(false)
edittext.maxLines = Integer.MAX_VALUE // Or your preferred fixed value
我经常需要这个功能,所以写了这个来保持代码的清洁:
fun EditText.multilineIme(action: Int) {
imeOptions = action
inputType = EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE
setHorizontallyScrolling(false)
maxLines = Integer.MAX_VALUE
}
// Then just call
edittext.multilineIme(EditorInfo.IME_ACTION_DONE)
如果你想在“完成”时添加一个可选的自定义操作,请尝试以下方法:
fun EditText.multilineDone(callback: (() -> Unit)? = null) {
val action = EditorInfo.IME_ACTION_DONE
multilineIme(action)
setOnEditorActionListener { _, actionId, _ ->
if (action == actionId) {
callback?.invoke()
true
}
false
}
}
}
// Then you can call
edittext.multilineDone { closeKeyboard() }
// or just
edittext.multilineDone()
需要在回调函数中轻松控制键盘?阅读此文章
然后在 EditText.multilineDone
中添加 hideKeyboard()
调用。
我认为这是做你的事情的方法。使用android:inputType="textMultiLine"
和android:imeOptions="actionDone"
会使回车键功能模糊不清。只需要记住你可以使用android:lines="10"
,并且可能删除android:inputType="textMultiLine"
,但这取决于你想要实现什么,有时你只需要android:inputType="textMultiLine"
而没有替代品。
EditText ed=new EditText(this);
ed.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_ENTER){
//do your stuff here
}
return false;
}
});
这对我来说似乎完美地运作了
int lineNum = 2;
mEditText.setHorizontallyScrolling(false);
mEditText.setLines(3);
keep this attributes on the EditText:
android:inputType="textMultiLine"
android:scrollHorizontally="false"
then add this code to only hide the keyboard when ENTER is pressed:
editText.setOnEditorActionListener(new OnEditorActionListener()
{
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)
{
editText.setSelection(0);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
return true;
}
else
{
return false;
}
}
});
如果不涉及屏幕键盘的外观,您可以在键盘上放置一个输入监听器,并在用户输入换行符时触发“完成”状态。