我正在尝试捕获此EditText上的键盘事件:
<EditText
android:id="@+id/deal_edit_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" >
这是我使用的代码:
EditText nameEdit = (EditText) rootView.findViewById(R.id.deal_edit_name);
nameEdit.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
Log.d("hello",keyCode+" "+ v.toString()+" "+ event.toString());
return false;
}
});
但是当我点击任何键(键盘或硬件)时,我的LogCat中没有显示任何内容。这有什么不对?
答案 0 :(得分:1)
你有没有尝试过这样的事情:
你必须返回true来获得事件
EditText nameEdit = (EditText) rootView.findViewById(R.id.deal_edit_name);
nameEdit.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_ENTER:
// do something
return true;
default:
break;
}
}
}
});
答案 1 :(得分:0)
试试这个
editText.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
Log.i(TAG,"Enter pressed");
}
else
Log.i(TAG,"button pressed");
return false;
}
});