带有完成操作按钮的多行EditText

kef*_*efs 97 android

是否有可能同时拥有一个EditText具有android:inputType="textMultiLine"集合的小部件android:imeOptions="actionDone"

我想要一个多行编辑框,键盘上的动作按钮完成,而不是回车(回车),但它似乎没有工作..

提前致谢

ale*_*btr 166

使用

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)

  • 为什么这与在xml中设置全部相反? (6认同)
  • 只是以编程方式需要 rawInputType,您仍然可以从 xml 设置 imeOptions `actionDone` (2认同)
  • 您也可以使用数据绑定从 XML 设置 rawInputType:`@BindingAdapter("rawInputType") fun setRawInputType(view: EditText, inputType: Int) { view.setRawInputType(inputType) }`。然后在布局 XML 的数据部分: `<import type="android.text.InputType" />` 和 EditText 中的 `app:rawInputType="@{InputType.TYPE_CLASS_TEXT}"`。 (2认同)

HYS*_*HYS 49

来自android文档:' "textMultiLine"普通文本键盘,允许用户输入包含换行符(回车符)的长字符串文本.'因此,如果要在键盘中使用"完成"按钮,则textMultiLine属性不合适.

使用完成按钮获取多行(在本例中为3行)输入字段的简单方法是使用EditText

android:lines="3" 
android:scrollHorizontally="false" 
Run Code Online (Sandbox Code Playgroud)

但是,出于某种原因,如果我在代码而不是布局文件(在onCreate中)中进行这些设置,这仅适用于我

TextView tv = (TextView)findViewById(R.id.editText);
if (tv != null) {
    tv.setHorizontallyScrolling(false);
    tv.setLines(3);
}
Run Code Online (Sandbox Code Playgroud)

我希望这对某人有所帮助,因为花了很长时间才弄明白.如果您找到一种方法使其从清单中运行,请告诉我们.

  • 我还建议尝试`maxLines()`而不是`setLines()`如果你想避免改变`EditText`的高度 (4认同)
  • 不适合我.这样做:http://stackoverflow.com/a/12570003/3268329 (2认同)

Ane*_*s U 24

工作实例!创建以下支持此功能的自定义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" />
Run Code Online (Sandbox Code Playgroud)


Oll*_*e C 15

要在 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)
}
Run Code Online (Sandbox Code Playgroud)

用法:

myEditText.setMultiLineCapSentencesAndDoneAction()
Run Code Online (Sandbox Code Playgroud)


Luk*_*kap 7

我认为这是做你事的方法.拥有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;
        }
});
Run Code Online (Sandbox Code Playgroud)


Gib*_*olt 7

可重复使用的 Kotlin 解决方案

在代码中设置这些值是唯一对我有用的东西

edittext.inputType = EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE
edittext.setHorizontallyScrolling(false)
edittext.maxLines = Integer.MAX_VALUE // Or your preferred fixed value
Run Code Online (Sandbox Code Playgroud)

我经常需要这个,所以这样做是为了保持代码干净:

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)
Run Code Online (Sandbox Code Playgroud)

如果您想在“完成”上添加可选的自定义操作,请尝试以下操作:

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()
Run Code Online (Sandbox Code Playgroud)

需要在回调中轻松控制键盘?阅读这篇文章

然后添加hideKeyboard()调用EditText.multilineDone

  • ☝️这是一个很好的答案 (2认同)

yon*_*nez 5

这似乎对我很有用

int lineNum = 2;
mEditText.setHorizontallyScrolling(false);
mEditText.setLines(3);
Run Code Online (Sandbox Code Playgroud)


小智 2

如果与屏幕键盘的外观无关,您可以简单地在键盘上放置一个输入侦听器,并在用户输入换行符时触发“完成”状态。


归档时间:

查看次数:

55587 次

最近记录:

6 年,8 月 前