EditText上的Done按钮的监听器?

时间:2011-04-15 13:33:10

标签: java android

如果我有一个EditText,我想听听用户是否按下键盘上的“完成”按钮..我该怎么做?

7 个答案:

答案 0 :(得分:108)

Dinash答案很好,但它并不适用于所有设备。下面的代码适用于所有设备

edittext.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            Toast.makeText(HelloFormStuff.this, edittext.getText(), Toast.LENGTH_SHORT).show();
            return true;
        }
        return false;
    }
});

答案 1 :(得分:82)

代码是

final EditText edittext = (EditText) findViewById(R.id.edittext);
edittext.setOnKeyListener(new View.OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                Toast.makeText(HelloFormStuff.this, edittext.getText(), Toast.LENGTH_SHORT).show();
                return true;
            }
            return false;
        }
    });

因为'edittext'是textfield的id

请查看此链接Simply set setOnKeyListener to your editText

答案 2 :(得分:13)

Kotlin扩展解决方案

在Kotlin中处理已完成操作的基本方法是:

edittext.setOnEditorActionListener { _, actionId, _ ->
    if (actionId == EditorInfo.IME_ACTION_DONE) {
        // Call onDone result here
        true
    }
    false
}

Kotlin扩展

使用它在您的主代码中调用edittext.onDone {/*action*/}。使其更具可读性和可维护性

edittext.onDone { submitForm() }

fun EditText.onDone(callback: () -> Unit) {
    // These lines optional if you don't want to set in Xml
    imeOptions = EditorInfo.IME_ACTION_DONE
    maxLines = 1
    setOnEditorActionListener { _, actionId, _ ->
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            callback.invoke()
            true
        }
        false
    }
}

如果不在代码中,请不要忘记将这些选项添加到您的edittext Xml中

<EditText ...
    android:imeOptions="actionDone"
    android:inputType="text"/>

如果您需要 inputType="textMultiLine" 支持,请read this post

答案 3 :(得分:2)

same Jone答案,但替换为lambda:

etPointCombatFirst.setOnEditorActionListener((v, actionId, event) -> {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    Toast.makeText(HelloFormStuff.this, edittext.getText(), Toast.LENGTH_SHORT).show();
                    return true;
                }
                return false;
            });

答案 4 :(得分:2)

根据Asad Rao的响应,我创建了此KOTLIN扩展功能。

fun TextView.onClickKeyboardDoneButton(funExecute: () -> Unit) {
    this.setOnEditorActionListener { _, actionId, _ ->
        when (actionId) {
            EditorInfo.IME_ACTION_DONE -> {
                funExecute.invoke()
                true
            }
            else -> false
        }
    }
}

使用:

myEditText.onClickKeyboardDoneButton{myFunctionToExecuteWhenUserClickDone()}

答案 5 :(得分:1)

此Kotlin版本应可在所有设备上使用:

editText.setOnEditorActionListener { _, actionId, _ ->
    if (actionId == EditorInfo.IME_ACTION_DONE) {
        // Do something
        true
    } else {
        false
    }
}

答案 6 :(得分:0)

接收方法:

fun EditText.onImeActionDoneClicks(): Observable<Unit> {
    return Observable.create { emitter ->
        setOnEditorActionListener { _, actionId, _ ->
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                emitter.onNext(Unit)
                true
            } else {
                false
            }
        }
    }
}

compositeDisposable += lastEditText.onImeActionDoneClicks().subscribe {
    toast("onImeDoneClicks")
}