Android,Studio的EditText控件的回车监听事件无效
2个回答
2016-11-12 · 学高端技术就来八维教育
关注
展开全部
使用方法: 在xml里EditText节点下增加
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myEditText"
android:hint="@string/addItemHint"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:imeOptions="actionDone"
android:singleLine="true">
</EditText>
打印捕获的actionId显示值为“6”,查imeOptions可以看到“6”对应“actionDone”。
打开本地帮助: adt/sdk/docs/reference/android/R.styleable.html#TextView_imeOptions
actionDone 0x00000006
The action key performs a "done" operation, closing the soft input method. Corresponds to IME_ACTION_DONE.
这是监听部分最后修改的代码
myEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Log.v("HAWKZY", "setOnEditorActionListener().onEditorAction(): enterring......");
if (actionId == EditorInfo.IME_ACTION_SEND
|| actionId == EditorInfo.IME_ACTION_DONE
|| (event != null
&& KeyEvent.KEYCODE_ENTER == event.getKeyCode()
&& KeyEvent.ACTION_DOWN == event.getAction())) {
String newItem = myEditText.getText().toString();
onNewItemAddedListener.onNewItemAdded(newItem);
myEditText.setText("");
return true;
}
return false;
}
});
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myEditText"
android:hint="@string/addItemHint"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:imeOptions="actionDone"
android:singleLine="true">
</EditText>
打印捕获的actionId显示值为“6”,查imeOptions可以看到“6”对应“actionDone”。
打开本地帮助: adt/sdk/docs/reference/android/R.styleable.html#TextView_imeOptions
actionDone 0x00000006
The action key performs a "done" operation, closing the soft input method. Corresponds to IME_ACTION_DONE.
这是监听部分最后修改的代码
myEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Log.v("HAWKZY", "setOnEditorActionListener().onEditorAction(): enterring......");
if (actionId == EditorInfo.IME_ACTION_SEND
|| actionId == EditorInfo.IME_ACTION_DONE
|| (event != null
&& KeyEvent.KEYCODE_ENTER == event.getKeyCode()
&& KeyEvent.ACTION_DOWN == event.getAction())) {
String newItem = myEditText.getText().toString();
onNewItemAddedListener.onNewItemAdded(newItem);
myEditText.setText("");
return true;
}
return false;
}
});
推荐于2018-03-30 · 知道合伙人互联网行家
关注
展开全部
android监听事件添加动作的三种方式: 第一种也是最开始就接触的方式,通常在activity组件的oncreate事件中直接定义,直接动作。这种方式每个控件都定义一次,通常不方便. Button btn = (Button) findViewById(R.id.myButton); btn .setOnClickL...
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询