Android 当用户未按下“完成”按钮时,是否侦听EditText上的“完成”按钮并清除EditText?

Android 当用户未按下“完成”按钮时,是否侦听EditText上的“完成”按钮并清除EditText?,android,android-edittext,focus,Android,Android Edittext,Focus,我有一个EditText,我想听听,如果用户按下键盘上的“完成”按钮,我还想在用户未按下软键盘上的“完成”按钮时清除EditText,我该怎么做?要检查用户按下软键盘上的“完成”按钮,请使用以下代码: edittext.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView textView, int i,

我有一个EditText,我想听听,如果用户按下键盘上的“完成”按钮,我还想在用户未按下软键盘上的“完成”按钮时清除EditText,我该怎么做?

要检查用户按下软键盘上的“完成”按钮,请使用以下代码:

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.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View view, boolean hasFocus) {
        if(!hasFocus){
            edittext.setText("");
        }
    }
});
要在焦点更改后清除edittext的文本,请使用以下代码:

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.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View view, boolean hasFocus) {
        if(!hasFocus){
            edittext.setText("");
        }
    }
});
在科特林,就像这样:

android:imeOptions="actionDone"

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

“当用户未按下“完成”按钮时”是什么意思?在哪种情况下,您想清除编辑文本?@cherif:是的,我想清除编辑文本。有时用户想放弃编辑文本上下文并关闭软键盘而不保存。我已更新了代码。请看一下。如果它解决了您的问题,请将其标记为一个答案,以便为其他人获得正确答案。哪一个是第一个?因为我将setOnFocusChangeListener放在第一位,所以setOnEditorActionListener是第二位。当我输入编辑文本,然后按“完成”按钮时,顺序并不重要。一旦您关注edittext或从edittext中删除焦点,SetOnFocusChangeListener将收到调用。一旦软键盘打开,setOnEditorActionListener将得到调用。你能检查一下它是否符合你的要求吗。