Android setOnEditorActionListener()不会触发

我试图设置一个监听器来确定EditText何时按下输入按钮,但是根本没有触发。我在装有Android 4.2.2的LG Nexus

4上进行了测试。setOnEditorActionListener可以在具有Android 2.3的Amazon Kindle

Fire上使用,并且setImeActionLabel无处可用!我也无法为Enter按钮设置文本。这是代码:

mEditText.setImeActionLabel("Reply", EditorInfo.IME_ACTION_UNSPECIFIED);

mEditText.setOnEditorActionListener(new OnEditorActionListener() {

@Override

public boolean onEditorAction(TextView v, int actionId,

KeyEvent event) {

Log.d("TEST RESPONSE", "Action ID = " + actionId + "KeyEvent = " + event);

return true;

}

});

我究竟做错了什么?我怎样才能解决这个问题?

回答:

您可以使用TextWatcher。

    editText.addTextChangedListener(new TextWatcher() {

@Override

public void onTextChanged(CharSequence s, int start, int before, int count) {

}

@Override

public void beforeTextChanged(CharSequence s, int start, int count,

int after) {

}

@Override

public void afterTextChanged(Editable s) {

if (s.charAt(s.length() - 1) == '\n') {

Log.d("TEST RESPONSE", "Enter was pressed");

}

}

});

以上是 Android setOnEditorActionListener()不会触发 的全部内容, 来源链接: utcz.com/qa/433385.html

回到顶部