How do I handle ImeOptions' done button click?
我在
1 | editText.setImeOptions(EditorInfo.IME_ACTION_DONE); |
当用户单击屏幕键盘上的完成按钮(完成输入)时,我想更改
从屏幕键盘上击中完成按钮时,我该如何跟踪?
我最终得到了罗伯茨和基拉格答案的结合:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | ((EditText)findViewById(R.id.search_field)).setOnEditorActionListener( new EditText.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { // Identifier of the action. This will be either the identifier you supplied, // or EditorInfo.IME_NULL if being called due to the enter key being pressed. if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE || event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) { onSearchAction(v); return true; } // Return true if you have consumed the action, else false. return false; } }); |
更新:
上面的代码有时会两次激活回调。相反,我选择了以下代码,这些代码是从Google聊天客户端获得的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { // If triggered by an enter key, this is the event; otherwise, this is null. if (event != null) { // if shift key is down, then we want to insert the ' ' char in the TextView; // otherwise, the default action is to send the message. if (!event.isShiftPressed()) { if (isPreparedForSending()) { confirmSendMessageIfNeeded(); } return true; } return false; } if (isPreparedForSending()) { confirmSendMessageIfNeeded(); } return true; } |
试试这个,它应该可以满足您的需求:
1 2 3 4 5 6 7 8 9 10 | editText.setOnEditorActionListener(new EditText.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_DONE) { //do here your stuff f return true; } return false; } }); |
1 2 | <EditText android:imeOptions="actionDone" android:inputType="text"/> |
然后,java代码是
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | edittext.setOnEditorActionListener(new OnEditorActionListener() { public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if ((actionId == EditorInfo.IME_ACTION_DONE)) { Log.i(TAG,"Here you can write the code"); return true; } return false; } }); |
我知道这个问题很老,但是我想指出对我有用的。
我尝试使用Android开发者网站上的示例代码(如下所示),但没有用。因此,我检查了EditorInfo类,并意识到将IME_ACTION_SEND整数值指定为
来自Android开发人员的示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | editTextEmail = (EditText) findViewById(R.id.editTextEmail); editTextEmail .setOnEditorActionListener(new OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { boolean handled = false; if (actionId == EditorInfo.IME_ACTION_SEND) { /* handle action here */ handled = true; } return handled; } }); |
因此,我将整数值添加到了
1 2 3 4 | <?xml version="1.0" encoding="utf-8"?> <resources> <integer name="send">0x00000004</integer> </resources> |
然后,我如下编辑布局文件
1 2 3 4 5 6 7 | <EditText android:id="@+id/editTextEmail" android:layout_width="match_parent" android:layout_height="wrap_content" android:imeActionId="@integer/send" android:imeActionLabel="@+string/send_label" android:imeOptions="actionSend" android:inputType="textEmailAddress"/> |
然后,示例代码起作用了。
虽然大多数人都直接回答了这个问题,但我想详细说明其背后的概念。首先,当我创建默认的登录活动时,引起了IME的注意。它为我生成了一些代码,其中包括以下内容:
1 2 3 4 5 6 7 8 9 10 11 | <EditText android:id="@+id/password" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/prompt_password" android:imeActionId="@+id/login" android:imeActionLabel="@string/action_sign_in_short" android:imeOptions="actionUnspecified" android:inputType="textPassword" android:maxLines="1" android:singleLine="true"/> |
您应该已经熟悉inputType属性。这只是告知Android所需的文本类型,例如电子邮件地址,密码或电话号码。可在此处找到所有可能值的完整列表。
但是,我不理解它的用途是属性
这样,您可以通过为
1 2 3 4 5 6 7 8 9 10 | editText.setOnEditorActionListener(new EditText.OnEditorActionListener() { @Override public boolean onEditorAction(EditText v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_DONE) { //do here your stuff f return true; } return false; } }); |
现在在我的示例中,我具有
1 2 3 4 5 6 7 8 9 10 11 | mPasswordView = (EditText) findViewById(R.id.password); mPasswordView.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) { if (id == R.id.login || id == EditorInfo.IME_NULL) { attemptLogin(); return true; } return false; } }); |
有关如何设置OnKeyListener以及让它侦听"完成"按钮的更多详细信息。
首先,将OnKeyListener添加到类的Implements部分。然后添加在OnKeyListener接口中定义的函数:
1 2 3 4 5 6 7 8 9 10 11 12 13 | /* * Respond to soft keyboard events, look for the DONE press on the password field. */ public boolean onKey(View v, int keyCode, KeyEvent event) { if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) { // Done pressed! Do something here. } // Returning false allows other listeners to react to the press. return false; } |
给定一个EditText对象:
1 2 | EditText textField = (EditText)findViewById(R.id.MyEditText); textField.setOnKeyListener(this); |
感谢Kotlin中的chikka.anddev和Alex Cohn,它是:
1 2 3 4 5 6 7 8 9 | text.setOnEditorActionListener { v, actionId, event -> if (actionId == EditorInfo.IME_ACTION_DONE || event?.action == KeyEvent.ACTION_DOWN && event.keyCode == KeyEvent.KEYCODE_ENTER) { doSomething() true } else { false } } |
在这里,我检查