我想知道是否有一种方法来处理用户在输入EditText时按下Enter,就像onSubmit HTML事件。

还想知道是否有一种方法来操纵虚拟键盘,以这样的方式,“完成”按钮被标记为其他的东西(例如“Go”),并在单击时执行特定的动作(再次,像onSubmit)。


当前回答

好吧,如果这些答案都对你不起作用,你还没生气,我有一个解决办法。 使用AppCompatMultiAutoCompleteTextView(是的!)而不是EditText与以下代码(kotlin)

val filter = InputFilter { source, start, end, _, _, _ ->
        var keepOriginal = true
        val sb = StringBuilder(end - start)
        for (i in start until end) {
            val c = source[i]
            if (c != '\n')
                sb.append(c)
            else {
                keepOriginal = false
                //TODO:WRITE YOUR CODE HERE
            }
        }
        if (keepOriginal) null else {
            if (source is Spanned) {
                val sp = SpannableString(sb)
                TextUtils.copySpansFrom(source, start, sb.length, null, sp, 0)
                sp
            } else {
                sb
            }
        }
    }

appCompatMultiAutoCompleteTextView.filters = arrayOf(filter);

它(可能)适用于所有设备,我在android 4.4和10上测试了它。它在小米中也起作用。 我他妈的♥机器人:)

其他回答

你也可以做到。

editText.setOnKeyListener(new OnKeyListener() {

            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event)
            {
                if (event.getAction() == KeyEvent.ACTION_DOWN
                        && event.getKeyCode() ==       KeyEvent.KEYCODE_ENTER) 
                {
                    Log.i("event", "captured");

                    return false;
                } 

            return false;
        }
    });
final EditText edittext = (EditText) findViewById(R.id.edittext);
edittext.setOnKeyListener(new OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        // If the event is a key-down event on the "enter" button
        if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
            (keyCode == KeyEvent.KEYCODE_ENTER)) {
          // Perform action on key press
          Toast.makeText(HelloFormStuff.this, edittext.getText(), Toast.LENGTH_SHORT).show();
          return true;
        }
        return false;
    }
});

如果使用DataBinding,请参见https://stackoverflow.com/a/52902266/2914140和https://stackoverflow.com/a/67933283/2914140。

Bindings.kt:

@BindingAdapter("onEditorEnterAction")
fun EditText.onEditorEnterAction(callback: OnActionListener?) {
    if (callback == null) setOnEditorActionListener(null)
    else setOnEditorActionListener { v, actionId, event ->
        val imeAction = when (actionId) {
            EditorInfo.IME_ACTION_DONE,
            EditorInfo.IME_ACTION_SEND,
            EditorInfo.IME_ACTION_GO -> true
            else -> false
        }

        val keydownEvent = event?.keyCode == KeyEvent.KEYCODE_ENTER 
            && event.action == KeyEvent.ACTION_DOWN

        if (imeAction or keydownEvent) {
            callback.enterPressed()
            return@setOnEditorActionListener true
        }
        return@setOnEditorActionListener false
    }
}

interface OnActionListener {
    fun enterPressed()
}

layout.xml:

<data>
    <variable
        name="viewModel"
        type="YourViewModel" />
</data>    

<EditText
    android:imeOptions="actionDone|actionSend|actionGo"
    android:singleLine="true"
    android:text="@={viewModel.message}"
    app:onEditorEnterAction="@{() -> viewModel.send()}" />

文本字段上的InputType必须是文本,以便CommonsWare所说的工作。刚刚尝试了所有这些,在试验之前没有inputType,没有任何工作,进入一直注册为软进入。在inputType = text之后,包括setImeLabel在内的所有东西都工作了。

示例:android:inputType="text"

我通过扩展新的MaterialAlertDialogBuilder创建了一个辅助类

使用

new InputPopupBuilder(context)
        .setInput(R.string.send, 
                R.string.enter_your_message, 
                text -> sendFeedback(text, activity))
        .setTitle(R.string.contact_us)
        .show();

Code

public class InputPopupBuilder extends MaterialAlertDialogBuilder {

    private final Context context;
    private final AppCompatEditText input;

    public InputPopupBuilder(Context context) {
        super(context);
        this.context = context;
        input = new AppCompatEditText(context);
        input.setInputType(InputType.TYPE_CLASS_TEXT);
        setView(input);
    }

    public InputPopupBuilder setInput(int actionLabel, int hint, Callback callback) {
        input.setHint(hint);
        input.setImeActionLabel(context.getString(actionLabel), KeyEvent.KEYCODE_ENTER);
        input.setOnEditorActionListener((TextView.OnEditorActionListener) (v, actionId, event) -> {
            if (actionId == EditorInfo.IME_NULL
                    && event.getAction() == KeyEvent.ACTION_DOWN) {
                Editable text = input.getText();
                if (text != null) {
                    callback.onClick(text.toString());
                    return true;
                }
            }
            return false;
        });

        setPositiveButton(actionLabel, (dialog, which) -> {
            Editable text = input.getText();
            if (text != null) {
                callback.onClick(text.toString());
            }
        });

        return this;
    }

    public InputPopupBuilder setText(String text){
        input.setText(text);
        return this;
    }

    public InputPopupBuilder setInputType(int inputType){
        input.setInputType(inputType);
        return this;
    }

    public interface Callback {
        void onClick(String text);
    }
}

需要

implementation 'com.google.android.material:material:1.3.0-alpha04'