Swa*_*ato 59
我不太确定在接受的答案中使用了哪种听众.我使用了OnKeyListener
附件EditText
,它无法抓住下一个也没有完成.
然而,使用OnEditorActionListener
工作,它还允许我通过比较动作值与定义的常量EditorInfo.IME_ACTION_NEXT
和来区分它们EditorInfo.IME_ACTION_DONE
.
editText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((actionId & EditorInfo.IME_MASK_ACTION) != 0) {
doSomething();
return true;
}
else {
return false;
}
}
});
Run Code Online (Sandbox Code Playgroud)
Jas*_*son 38
@ Swato的回答对我来说并不完整(并且没有编译!)所以我展示了如何对DONE和NEXT动作进行比较.
editText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
int result = actionId & EditorInfo.IME_MASK_ACTION;
switch(result) {
case EditorInfo.IME_ACTION_DONE:
// done stuff
break;
case EditorInfo.IME_ACTION_NEXT:
// next stuff
break;
}
}
});
Run Code Online (Sandbox Code Playgroud)
另外我想指出,对于JellyBean,需要更高的OnEditorActionListener来监听'enter'或'next'而你不能使用OnKeyListener.来自文档:
由于软输入方法可以使用多种创造性的输入文本方式,因此无法保证软键盘上的任何按键都会产生关键事件:这由IME自行决定,实际上不鼓励发送此类事件.您永远不应该依赖于在软输入方法上接收任何键的KeyEvent.
参考:http://developer.android.com/reference/android/view/KeyEvent.html
Mia*_*rke 31
注意:这个答案很旧,不再适用.请参阅以下答案.
您捕获KeyEvent然后检查其密钥代码.FLAG_EDITOR_ACTION用于识别来自IME的输入密钥,其输入密钥已被自动标记为"下一个"或"已完成"
if (event.getKeyCode() == KeyEvent.FLAG_EDITOR_ACTION)
//your code here
Run Code Online (Sandbox Code Playgroud)
在这里找到文档.
Ara*_* GM 12
就像这样:
editText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
if(actionId == EditorInfo.IME_ACTION_DONE)
{
//Do Something
}
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
小智 8
etSearchFriends = (EditText) findViewById(R.id.etSearchConn);
etSearchFriends.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
Toast.makeText(ACTIVITY_NAME.this, etSearchFriends.getText(),Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
51559 次 |
最近记录: |