EditText 上的 Done 按钮的侦听器并在用户未按 Done 按钮时清除 EditText?

Listener for Done button on EditText and clear EditText when user not press Done button?

我有一个 EditText,我想监听用户是否按下键盘上的 "done" 按钮,我还想在用户未按下软键盘上的 "done" 按钮时清除 EditText ,我该怎么做?

要检查用户是否按下了软键盘的 "Done" 按钮,请使用以下代码:

edittext.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
        if(i== EditorInfo.IME_ACTION_DONE){
            Toast.makeText(getApplicationContext(),"Done pressed",Toast.LENGTH_SHORT).show();
        }
        return false;
    }
});

要在更改焦点后清除 edittext 的文本,请使用以下代码:

edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View view, boolean hasFocus) {
        if(!hasFocus){
            edittext.setText("");
        }
    }
});

在 Kotlin 中是这样的:

android:imeOptions="actionDone"

edittext.onFocusChangeListener = OnFocusChangeListener { view, hasFocus ->
                if (hasFocus) {
                    //Logic Here
                }
            }