The case of "EditText + button" forming an "input + key response" is the most common in Android programming.
But there are some details to note:
- After EditText input, click button to request, the soft keyboard should disappear on its own
- After edittext input, do not click on the button to request, but directly click on the soft keyboard "enter", then should also be able to respond to the request
For issue 1, you can actively hide the soft keyboard in response to the OnClick event of the button, adding the following code
[Java]View Plaincopy
- Inputmethodmanager IMM = (inputmethodmanager) getsystemservice (Context.input_method_service);
- Imm.hidesoftinputfromwindow (Medittext.getwindowtoken (), 0);
For question 2, you can find the answer in the EditText API doc
[Plain] view plaincopy
- public void setoneditoractionlistener ( textview.oneditoractionlistener l)
-
- set a Special listener to be called when an action is performed on the text view. this will be called when the enter key is pressed, or when an action supplied to the ime is selected by the user. setting this means that the normal hard key event will not insert a newline into the text view, even if it is multi-line; holding down the ALT modifier will, however, allow the user to insert a newline character.
So, just need to set a oneditoractionlistener for edittext, simple examples such as the next
[Java]View Plaincopy
- medittext.setoneditoractionlistener (new Textview.oneditoractionlistener () {
- @Override
- public boolean Oneditoraction (textview v, int actionid, keyevent event) {
- //todo here to Do "enter" response processing
- return true;
- }
-
Note: The Textview.oneditoractionlistener interface method oneditoraction The second parameter of the method ActionId, and its possible values can be found in the description of the Editorinfo. Listed below:
Ime_action_done |
Ime_action_go |
Ime_action_next |
Ime_action_none |
Ime_action_previous |
Ime_action_search |
Ime_action_send |
Ime_action_unspecified |
The ENTER key of the soft keyboard displays the "Done" text by default, and we know that pressing ENTER indicates that the predecessor work is ready and what to go for. For example, in a search, we enter the text to search for, and then press ENTER to go to search, but the default enter to display the "done" text, looking not very suitable, does not conform to the semantics of the search, if you can display "search" two words or display a search icon is much better. It turns out that our ideas are reasonable and Android also provides us with such features. Change the default "done" text by setting android:imeoptions. Here are some common constant values:
- Actionunspecified unspecified, corresponding to constant editorinfo.ime_action_unspecified. Effect:
- Actionnone no action, corresponding to the constant Editorinfo.ime_action_none effect:
- Actiongo, corresponding to the constant Editorinfo.ime_action_go effect:
- Actionsearch Search, corresponding to the constant Editorinfo.ime_action_search effect:
- Actionsend send, corresponding to the constant Editorinfo.ime_action_send effect:
- Actionnext next, corresponding to the constant Editorinfo.ime_action_next effect:
- Actiondone complete, corresponding to the constant Editorinfo.ime_action_done effect:
The following is an example of a search, illustrating an instance, modifying Main.xml as follows:
XML code
- <? XML version= "1.0" encoding="Utf-8"?>
- <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <EditText
- android:id="@+id/edit_text"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:imeoptions="Actionsearch"/>
- </linearlayout>
Modify the Helloedittext as follows:
Java code
- Package Com.flysnow;
- Import android.app.Activity;
- Import Android.os.Bundle;
- Import android.view.KeyEvent;
- Import Android.widget.EditText;
- Import Android.widget.TextView;
- Import Android.widget.Toast;
- Import Android.widget.TextView.OnEditorActionListener;
- Public class Helloedittext extends Activity {
- /** Called when the activity is first created. * /
- @Override
- public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- EditText edittext= (EditText) Findviewbyid (R.id.edit_text);
- Edittext.setoneditoractionlistener (new Oneditoractionlistener () {
- @Override
- Public Boolean oneditoraction (TextView V, int ActionId, keyevent event) {
- Toast.maketext (Helloedittext. This, string.valueof (ActionId), Toast.length_short). Show ();
- return false;
- }
- });
- }
- }
Run the program and click Enter (that is, the Search icon Soft keyboard button) to display the ActionId. Each of the above settings corresponds to a constant, where the ActionId is the constant value.
Some control of Android soft keyboard turn, the format is a bit messy