Implement Android Press ENTER to hide the input keyboard, there are two ways:
1.) If the layout is multiple edittext, set android:singleline= "true" for each EditText control, and the floppy disk input key is next, until the last one gets the focus after it is done, after clicking Done, The floppy disk input keyboard is hidden. or set the Imeoptions property of the EditText android:imeoptions= "Actiondone", whether it is the last edittext or not, click Enter to hide the IME.
2.) Listen for the Enter event and write the event response for enter. Set the Onkeylistener of the text box, when KeyCode ==keyevent.keycode_enter, indicates that the ENTER key is pressed, you can write your own event response function.
Click to download the source code
Specific code:
View Plaincopy to Clipboardprint?
- Package Listenter.main;
- Import android.app.Activity;
- Import Android.content.Context;
- Import Android.os.Bundle;
- Import android.view.KeyEvent;
- Import Android.view.View;
- Import Android.view.View.OnKeyListener;
- Import Android.view.inputmethod.InputMethodManager;
- Import Android.widget.EditText;
- Public class enterlistenter extends Activity {
- /** Called when the activity is first created. * /
- @Override
- Public void onCreate (Bundle savedinstancestate) {
- Super . OnCreate (savedinstancestate);
- Setcontentview (R.layout.main);
- EditText password= (EditText) Findviewbyid (R.id.password);
- Password.setonkeylistener (OnKey);
- }
- Onkeylistener onkey= New Onkeylistener () {
- @Override
- Public boolean OnKey (View V, int keycode, KeyEvent event) {
- //TODO auto-generated method stub
- if (keycode = = keyevent.keycode_enter) {
- Inputmethodmanager IMM = (Inputmethodmanager) v.getcontext (). Getsystemservice (Context.input_method_service);
- if (Imm.isactive ()) {
- Imm.hidesoftinputfromwindow (V.getapplicationwindowtoken (), 0 );
- }
- return true;
- }
- return false;
- }
- };
- }
[Java]View Plaincopyprint?
- Package Listenter.main;
- Import android.app.Activity;
- Import Android.content.Context;
- Import Android.os.Bundle;
- Import android.view.KeyEvent;
- Import Android.view.View;
- Import Android.view.View.OnKeyListener;
- Import Android.view.inputmethod.InputMethodManager;
- Import Android.widget.EditText;
- Public class enterlistenter extends Activity {
- /** Called when the activity is first created. * /
- @Override
- Public void onCreate (Bundle savedinstancestate) {
- Super . OnCreate (savedinstancestate);
- Setcontentview (R.layout.main);
- EditText password= (EditText) Findviewbyid (R.id.password);
- Password.setonkeylistener (OnKey);
- }
- Onkeylistener onkey=New Onkeylistener () {
- @Override
- Public boolean OnKey (View V, int keycode, KeyEvent event) {
- //TODO auto-generated method stub
- if (keycode = = keyevent.keycode_enter) {
- Inputmethodmanager IMM = (Inputmethodmanager) v.getcontext (). Getsystemservice (Context.input_method_service);
- if (Imm.isactive ()) {
- Imm.hidesoftinputfromwindow (V.getapplicationwindowtoken (), 0 );
- }
- return true;
- }
- return false;
- }
- };
- }
Figure-1 Click Enter to hide the keyboard
Ps:
Set the EditText IME options property to a different value, and you can display different text or patterns on the ENTER key
Actionnone: Enter, press the cursor to the next line
Actiongo:go,
Actionsearch: A magnifying glass
Actionsend:send
Actionnext:next
Actiondone:done, hiding the soft keyboard, even if it is not the last text input box