我有一个包含编辑文字的对话框。我想在用户在键盘上单击完成时执行操作。我的代码看起来像
我的编辑文字xml看起来像
<EditText
android:id="@+id/commondialog_userinput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:imeOptions="actionDone"
android:selectAllOnFocus="true"
android:inputType="text" />
和添加的监听器是
final EditText inputField = (EditText)dialog.findViewById(R.id.commondialog_userinput);
inputField.setInputType(EditorInfo.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
inputField.setText(AndroidGlobalVariables.getDocumentName(), TextView.BufferType.EDITABLE);// No I18N
inputField.setFocusableInTouchMode(true);
inputField.requestFocus();
inputField.selectAll();
inputField.setOnKeyListener(new View.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(EditorActivity.getActivity(), inputField.getText(),Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
答案 0 :(得分:2)
将setOnEditorActionListener用于您想要在键盘上执行操作的EditText,如下所示:
your_editText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE)
{
** PUT YOUR ACTION HERE !!! **
}
return false;
}
});
答案 1 :(得分:0)
您需要的是http://developer.android.com/reference/android/view/inputmethod/EditorInfo.html#IME_ACTION_DONE。不同的Android实现和版本可能会使用不同的密钥,您不能依赖“输入”密钥代码。
答案 2 :(得分:0)
将其用于编辑文本侦听器
e_inputField .setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView arg0,
int actionId, KeyEvent arg2) {
// TODO Auto-generated method stub
if (actionId == EditorInfo.IME_ACTION_NEXT) {
//do your stuff here
}
return false;
}
});