Some Control of android Soft Keyboard

Keywords: Android xml Programming encoding

Original: http://blog.csdn.net/wang_shaner/article/details/8467688

The case of "EditText + Button" forming an "input + keystroke response" is presented in the Android Programming is the most common.

But there are some details to be noted:

  1. After entering EditText, click Button to make the request, and the soft keyboard should disappear by itself.
  2. After entering EditText, instead of clicking Button to make a request, you can click "Enter" directly on the soft keyboard, so you should be able to respond to the request normally.
For Question 1, you can actively hide the soft keyboard in response to Button's onClick event by adding the following code
  1. InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);  
  2. imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);  
For question 2, you can find the answer in EditText's api doc
  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 set up an onEditor ActionListener for EditText. A simple example is as follows
  1. mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {  
  2.     @Override  
  3.     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {  
  4.         //TODO "Enter" Response Processing  
  5.         return true;  
  6.     }  
  7. });  
Note: actionId, the second parameter of the TextView.OnEditorActionListener interface method onEditorAction method, can be found in the instructions of EditorInfo. The following are listed:
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 by default displays the "finished" text. We know that presenting Enter means that the pre-work is ready and what to go to. For example, in a search, we enter the text to be searched and press Enter to indicate that we are going to search, but the default Enter key shows the "completed" text, which is not suitable to look at and does not match the semantics of the search. It would be better if we could display the word "search" or an icon to represent the search. It turns out that our idea is reasonable, and Android also provides us with such functions. Change the default "done" text by setting android:imeOptions. Here are some common constants:

  1. actionUnspecified is not specified, corresponding to the constant EditorInfo.IME_ACTION_UNSPECIFIED.Effect:
  2. actionNone has 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 Search Search for Constant EditorInfo.IME_ACTION_SEARCH Effect:
  5. actionSend sent, corresponding to the constant EditorInfo.IME_ACTION_SEND effect:
  6. The next action next corresponds to the constant EditorInfo.IME_ACTION_NEXT effect:
  7. actionDone completed, corresponding to the constant EditorInfo.IME_ACTION_DONE effect:

The following search example demonstrates an example by modifying main.xml as follows:

  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 HelloEditText as follows:

  1. package com.flysnow;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.KeyEvent;  
  6. import android.widget.EditText;  
  7. import android.widget.TextView;  
  8. import android.widget.Toast;  
  9. import android.widget.TextView.OnEditorActionListener;  
  10.   
  11. public class HelloEditText extends Activity {  
  12.     /** Called when the activity is first created. */  
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.main);  
  17.         EditText editText=(EditText)findViewById(R.id.edit_text);  
  18.         editText.setOnEditorActionListener(new OnEditorActionListener() {  
  19.             @Override  
  20.             public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {  
  21.                 Toast.makeText(HelloEditText.this, String.valueOf(actionId), Toast.LENGTH_SHORT).show();  
  22.                 return false;  
  23.             }  
  24.         });  
  25.     }  
  26. }  

Run the program and click Enter (that is, search icon soft keyboard button) to display the actionId. Each of our settings above corresponds to a constant, where actionId is the constant.

Posted by bampot on Mon, 01 Apr 2019 01:24:29 -0700