【问题标题】:Android - Handle "Enter" in an EditTextAndroid - 在 EditText 中处理“Enter”
【发布时间】:2010-12-02 04:04:13
【问题描述】:

我想知道是否有办法处理用户在输入EditText 时按下Enter,类似于onSubmit HTML 事件。

还想知道是否有一种方法可以操纵虚拟键盘,使“完成”按钮被标记为其他内容(例如“开始”)并在单击时执行特定操作(再次,例如 onSubmit)。

【问题讨论】:

标签: android android-edittext textview


【解决方案1】:

我通过扩展新的MaterialAlertDialogBuilder为此创建了一个帮助类

用法

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

代码

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'

【讨论】:

    【解决方案2】:

    好的,如果没有一个答案对你有用并且还没有生气,我有一个解决方案。 在这段代码中使用 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 中对其进行了测试。它也适用于小米。 我该死的♥机器人:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-06
      • 1970-01-01
      • 1970-01-01
      • 2011-12-14
      • 2012-12-26
      • 2012-02-22
      • 1970-01-01
      • 2012-10-04
      相关资源
      最近更新 更多