簡體   English   中英

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

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

我在按下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;
}

您應該檢查onKey方法是否真正被調用以及向其傳遞了哪些鍵代碼。 根據您EditText的輸入類型,可能會生成與KEYCODE_ENTER不同的事件。 您也可以嘗試使用OnEditorActionListener代替OnKeyListener。

嘗試這個:

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