Keyboard-app three ways to monitor soft keyboard keys

Source: Internet
Author: User

Objective:

We sometimes encounter on the Android phone when listening to soft keyboard keys, such as: We can click on the browser to enter the URL after the "GO" button in the lower right corner of the soft keyboard to load the URL page, click on the Search box, the bottom right corner of the search symbol can be searched , or after all data has been entered, click "Done" in the lower right corner to proceed immediately.

:























Function 1: Rewrite the activity of the Dispatchkeyevent (KeyEvent event) method, in which the Listener Keyeventkey.keycode_enter key (the bottom right corner of the OK key), when this key is pressed, Hides the IME soft keyboard, sets EditText content, and loads WebView content. [Java]View Plaincopy
  1. @Override
  2. Public Boolean dispatchkeyevent (KeyEvent event) {
  3. if (event.getkeycode () = = Keyevent.keycode_enter) {
  4. / * Hide Soft keyboard * /
  5. Inputmethodmanager Inputmethodmanager = (inputmethodmanager) getsystemservice (Context.input_method_service);
  6. if (inputmethodmanager.isactive ()) {
  7. Inputmethodmanager.hidesoftinputfromwindow (mainactivity.   This.getcurrentfocus (). Getwindowtoken (), 0);
  8. }
  9. Edittext.settext ("Success");
  10. Webview.loadurl (URL);
  11. return true;
  12. }
  13. return super.dispatchkeyevent (event);
  14. }
Function 2: The method of rewriting Dispatchkeyevent (KeyEvent event) feels a bit sledgehammer, because we are very likely to do other tasks in this method, so we can use the Onkeylistener method to listen for the soft keyboard keys. [Java]View Plaincopy
  1. Private Onkeylistener Onkeylistener = new Onkeylistener () {
  2. @Override
  3. Public Boolean OnKey (View V, int keycode, keyevent event) {
  4. if (keycode = = Keyevent.keycode_enter && event.getaction () = = Keyevent.action_down) {
  5. / * Hide Soft keyboard * /
  6. Inputmethodmanager Inputmethodmanager = (inputmethodmanager) getsystemservice (Context.input_method_service);
  7. if (inputmethodmanager.isactive ()) {
  8. Inputmethodmanager.hidesoftinputfromwindow (V.getapplicationwindowtoken (), 0);
  9. }
  10. Edittext.settext ("Success");
  11. Webview.loadurl (URL);
  12. return true;
  13. }
  14. return false;
  15. }
  16. };
[Java]View Plaincopy
    1. Edittext.setonkeylistener (Onkeylistener);
Function 3:

The third method I think can help programmers more accurately judge the lower right corner of the button, in order to deal with more complex situations. It helps programmers make more granular actions based on the "GO", "Done", "search" keys under the current message.

[Java]View Plaincopy
  1. Edittext.setoneditoractionlistener (new Textview.oneditoractionlistener () {
  2. @Override
  3. Public Boolean oneditoraction (TextView V, int ActionId, keyevent event) {
  4. / * Determine if the "GO" key is * /
  5. if (ActionId = = Editorinfo.ime_action_go) {
  6. / * Hide Soft keyboard * /
  7. Inputmethodmanager IMM = (Inputmethodmanager) v
  8. . GetContext (). Getsystemservice (
  9. Context.input_method_service);
  10. if (imm.isactive ()) {
  11. Imm.hidesoftinputfromwindow (
  12. V.getapplicationwindowtoken (), 0);
  13. }
  14. Edittext.settext ("Success");
  15. Webview.loadurl (URL);
  16. return true;
  17. }
  18. return false;
  19. }
  20. });

Change the soft keyboard to the lower right corner to determine the key style: Soft keyboard Input method is not a constant, such as its lower right corner of the "OK" button, when the search box will become a search icon with the button, in the browser address bar will become "GO" key, When we write the app, we may also set the input method's "OK" key depending on the situation, and change the method is to set the Imeoptions property of the EditText control to a different value (the ENTER key can display different text and patterns). [HTML]View Plaincopy
  1. <EditText
  2. android:id="@+id/edittext"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:singleline="true"
  6. android:imeoptions="Actionsearch"/>
Actionnone: Enter, press the cursor to the next line
Actiongo:go,
Actionsearch: Magnifying Glass
Actionsend:send
Actionnext:next
Actiondone:done, OK/finish, hide soft keyboard, even if not the last text input box digression:

When I was writing this demo, I found a problem with webview, that the direct use of the Webview.load (URL) method would pop up the system browser on the phone to access the URL link instead of the webview we set up. The solution I found was to use Webview.setwebviewclient (...). method to ensure that the URL is loaded on the webview of the activity.

demo:http://download.csdn.net/detail/zhufuing/6903671

Keyboard-app three ways to monitor soft keyboard keys

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.