Some control of Android soft keyboard turn, the format is a bit messy

Source: Internet
Author: User

The case of "EditText + button" forming an "input + key response" is the most common in Android programming.

But there are some details to note:

    1. After EditText input, click button to request, the soft keyboard should disappear on its own
    2. 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
    1. Inputmethodmanager IMM = (inputmethodmanager) getsystemservice (Context.input_method_service);
    2. Imm.hidesoftinputfromwindow (Medittext.getwindowtoken (), 0);
For question 2, you can find the answer in the EditText API doc [Plain] view plaincopy  
    1. public void setoneditoractionlistener  ( textview.oneditoractionlistener l)   
    2.   
    3. 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
    1. medittext.setoneditoractionlistener (new  Textview.oneditoractionlistener ()  {  
    2.      @Override   
    3.     public boolean  Oneditoraction (textview v, int actionid, keyevent event)  {   
    4.          //todo  here to Do "enter" response processing   
    5.         return  true;  
    6.     }  
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:

    1. Actionunspecified unspecified, corresponding to constant editorinfo.ime_action_unspecified. Effect:
    2. Actionnone no action, corresponding to the constant Editorinfo.ime_action_none effect:
    3. Actiongo, corresponding to the constant Editorinfo.ime_action_go effect:
    4. Actionsearch Search, corresponding to the constant Editorinfo.ime_action_search effect:
    5. Actionsend send, corresponding to the constant Editorinfo.ime_action_send effect:
    6. Actionnext next, corresponding to the constant Editorinfo.ime_action_next effect:
    7. 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
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <EditText
  8. android:id="@+id/edit_text"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:imeoptions="Actionsearch"/>
  12. </linearlayout>

Modify the Helloedittext as follows:

Java code
  1. Package Com.flysnow;
  2. Import android.app.Activity;
  3. Import Android.os.Bundle;
  4. Import android.view.KeyEvent;
  5. Import Android.widget.EditText;
  6. Import Android.widget.TextView;
  7. Import Android.widget.Toast;
  8. Import Android.widget.TextView.OnEditorActionListener;
  9. Public class Helloedittext extends Activity {
  10. /** Called when the activity is first created. * /
  11. @Override
  12. public void OnCreate (Bundle savedinstancestate) {
  13. super.oncreate (savedinstancestate);
  14. Setcontentview (R.layout.main);
  15. EditText edittext= (EditText) Findviewbyid (R.id.edit_text);
  16. Edittext.setoneditoractionlistener (new Oneditoractionlistener () {
  17. @Override
  18. Public Boolean oneditoraction (TextView V, int ActionId, keyevent event) {
  19. Toast.maketext (Helloedittext.   This, string.valueof (ActionId), Toast.length_short). Show ();
  20. return false;
  21. }
  22. });
  23. }
  24. }

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

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.