Android Enter event programming

Source: Internet
Author: User

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?
  1. Package   Listenter.main;
  2. Import   android.app.Activity;
  3. Import   Android.content.Context;
  4. Import   Android.os.Bundle;
  5. Import   android.view.KeyEvent;
  6. Import   Android.view.View;
  7. Import   Android.view.View.OnKeyListener;
  8. Import   Android.view.inputmethod.InputMethodManager;
  9. Import   Android.widget.EditText;
  10. Public class enterlistenter extends Activity {
  11. /** Called when the activity is first created. * /   
  12. @Override   
  13. Public void onCreate (Bundle savedinstancestate) {
  14. Super   . OnCreate (savedinstancestate);
  15. Setcontentview (R.layout.main);
  16. EditText password= (EditText) Findviewbyid (R.id.password);
  17. Password.setonkeylistener (OnKey);
  18. }
  19. Onkeylistener onkey= New Onkeylistener () {
  20. @Override   
  21. Public boolean OnKey (View V, int keycode, KeyEvent event) {
  22. //TODO auto-generated method stub   
  23. if (keycode = = keyevent.keycode_enter) {
  24. Inputmethodmanager IMM = (Inputmethodmanager) v.getcontext (). Getsystemservice (Context.input_method_service);
  25. if (Imm.isactive ()) {
  26. Imm.hidesoftinputfromwindow (V.getapplicationwindowtoken (), 0 );
  27. }
  28. return    true;
  29. }
  30. return    false;
  31. }
  32. };
  33. }
[Java]View Plaincopyprint?
  1. Package  Listenter.main;
  2. Import  android.app.Activity;
  3. Import  Android.content.Context;
  4. Import  Android.os.Bundle;
  5. Import  android.view.KeyEvent;
  6. Import  Android.view.View;
  7. Import  Android.view.View.OnKeyListener;
  8. Import  Android.view.inputmethod.InputMethodManager;
  9. Import  Android.widget.EditText;
  10. Public class enterlistenter extends Activity {
  11. /** Called when the activity is first created. * /   
  12. @Override   
  13. Public void onCreate (Bundle savedinstancestate) {
  14. Super  . OnCreate (savedinstancestate);
  15. Setcontentview (R.layout.main);
  16. EditText password= (EditText) Findviewbyid (R.id.password);
  17. Password.setonkeylistener (OnKey);
  18. }
  19. Onkeylistener onkey=New Onkeylistener () {
  20. @Override   
  21. Public boolean OnKey (View V, int keycode, KeyEvent event) {
  22. //TODO auto-generated method stub   
  23. if (keycode = = keyevent.keycode_enter) {
  24. Inputmethodmanager IMM = (Inputmethodmanager) v.getcontext (). Getsystemservice (Context.input_method_service);
  25. if (Imm.isactive ()) {
  26. Imm.hidesoftinputfromwindow (V.getapplicationwindowtoken (), 0 );
  27. }
  28. return   true;
  29. }
  30. return   false;
  31. }
  32. };
  33. }

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.