简体   繁体   English

按下enter后如何清除edittext中的字符

[英]how to clear the character in edittext after enter is pressed

I am using the following code to clear edit text after enter is pressed, but its not working. 我在按下Enter键后使用下面的代码清除编辑文本,但无法正常工作。

inText.setOnKeyListener(this);
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
    if ((event.getAction() == KeyEvent.ACTION_DOWN)) {
        if (keyCode == KeyEvent.KEYCODE_ENTER) {
            inText.setText("");
        }
        return true;
    }
    return false;
}

You should check if the onKey method is actually called and what key codes are delivered to it. 您应该检查onKey方法是否真正被调用以及向其传递了哪些键代码。 It is possible that a different event than KEYCODE_ENTER is generated, depending on the input type of you EditText. 根据您EditText的输入类型,可能会生成与KEYCODE_ENTER不同的事件。 You could also try using OnEditorActionListener instead of OnKeyListener. 您也可以尝试使用OnEditorActionListener代替OnKeyListener。

try this: 尝试这个:

inText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                inText.setText("");
            }
            return false;
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM