android 虚拟键盘控制

软键盘显示的原理

  1. 软键盘的本质是什么?软键盘其实是一个Dialog
  2. InputMethodService为我们的输入法创建了一个Dialog,并且将该DialogWindow的某些参数(如Gravity)进行了设置,使之能够在底部或者全屏显示。当我们点击输入框时,系统对活动主窗口进行调整,从而为输入法腾出相应的空间,然后将该Dialog显示在底部,或者全屏显示。

(一) 软键盘的隐藏与出现

// view是需要控制的view对象

/**
     * 收起软键盘
     */
    public static void collapseSoftInputMethod(Context context, View v) {
        if (v != null) {
            InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }
    }

    /**
     * 显示软键盘
     */
    public static void showSoftInputMethod(Context context, View v) {
        if (v != null) {
            InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(v, 0);
        }
    }

(二) 软键盘的各个属性值含义

活动的主窗口如何与包含屏幕上的软键盘窗口交互。这个属性的设置将会影响两件事情:

1>     软键盘的状态——是否它是隐藏或显示——当活动(Activity)成为用户关注的焦点。

2>     活动的主窗口调整——是否减少活动主窗口大小以便腾出空间放软键盘或是否当活动窗口的部分被软键盘覆盖时它的内容的当前焦点是可见的。

它的设置必须是下面列表中的一个值,或一个”state…”值加一个”adjust…”值的组合。在任一组设置多个值——多个”state…”values,例如&mdash有未定义的结果。各个值之间用|分开。例如: <activity android:windowSoftInputMode=”stateVisible|adjustResize” . . . >

在这设置的值(除”stateUnspecified”和”adjustUnspecified”以外)将覆盖在主题中设置的值

描述

“stateUnspecified”

软键盘的状态(是否它是隐藏或可见)没有被指定。系统将选择一个合适的状态或依赖于主题的设置。

这个是为了软件盘行为默认的设置。

 

“stateUnchanged”

软键盘被保持无论它上次是什么状态,是否可见或隐藏,当主窗口出现在前面时。

“stateHidden”

当用户选择该Activity时,软键盘被隐藏——也就是,当用户确定导航到该Activity时,而不是返回到它由于离开另一个Activity。

“stateAlwaysHidden”

软键盘总是被隐藏的,当该Activity主窗口获取焦点时。

“stateVisible”

软键盘是可见的,当那个是正常合适的时(当用户导航到Activity主窗口时)。

“stateAlwaysVisible”

当用户选择这个Activity时,软键盘是可见的——也就是,也就是,当用户确定导航到该Activity时,而不是返回到它由于离开另一个Activity。

“adjustUnspecified”

它不被指定是否该Activity主窗口调整大小以便留出软键盘的空间,或是否窗口上的内容得到屏幕上当前的焦点是可见的。系统将自动选择这些模式中一种主要依赖于是否窗口的内容有任何布局视图能够滚动他们的内容。如果有这样的一个视图,这个窗口将调整大小,这样的假设可以使滚动窗口的内容在一个较小的区域中可见的。这个是主窗口默认的行为设置。

“adjustResize”

该Activity主窗口总是被调整屏幕的大小以便留出软键盘的空间

“adjustPan”

Activity主窗口并不调整屏幕的大小以便留出软键盘的空间。相反,当前窗口的内容将自动移动以便当前焦点从不被键盘覆盖和用户能总是看到输入内容的部分。这个通常是不期望比调整大小,因为用户可能关闭软键盘以便获得与被覆盖内容的交互操作。

 (三)软键盘与EditText交互

"EditText + Button"  形成一个 "输入+按键响应" 的案例在android编程中是最常见不过的了。

但还有一些细节需要注意:

  1. 在EditText输入后,点击Button进行请求,软键盘应该自行消失
  2. 在EditText输入后,不点击Button进行请求,而是直接点击软键盘上的"回车",那么也应该能够正常响应请求
针对问题1,可以在响应Button的onClick事件中,主动将软键盘隐藏,加入如下代码即可
[java] view plaincopy
 
  1. InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);  
  2. imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);  
针对问题2,可以在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.  
因此,只需要给EditText设置一个onEditorActionListener就好了,简单示例如下
[java] view plaincopy
 
  1. mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {  
  2.     @Override  
  3.     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {  
  4.         //TODO 这里做"回车"响应处理  
  5.         return true;  
  6.     }  
  7. });  
备注一下:TextView.OnEditorActionListener接口方法onEditorAction方法的第二个参数actionId,其可能的值在EditorInfo的说明中能够找到。列举如下:
IME_ACTION_DONE
IME_ACTION_GO
IME_ACTION_NEXT
IME_ACTION_NONE
IME_ACTION_PREVIOUS
IME_ACTION_SEARCH
IME_ACTION_SEND
IME_ACTION_UNSPECIFIED
 
 

软键盘的Enter键默认显示的是“完成”文本,我们知道按Enter建表示前置工作已经准备完毕了,要去什么什么啦。比如,在一个搜索中,我们输入要搜索的文本,然后按Enter表示要去搜索了,但是默认的Enter键显示的是“完成”文本,看着不太合适,不符合搜索的语义,如果能显示“搜索”两个字或者显示一个表示搜索的图标多好。事实证明我们的想法是合理的,Android也为我们提供的这样的功能。通过设置android:imeOptions来改变默认的“完成”文本。这里举几个常用的常量值:

  1. actionUnspecified  未指定,对应常量EditorInfo.IME_ACTION_UNSPECIFIED.效果:
  2. actionNone 没有动作,对应常量EditorInfo.IME_ACTION_NONE 效果:
  3. actionGo 去往,对应常量EditorInfo.IME_ACTION_GO 效果:
  4. actionSearch 搜索,对应常量EditorInfo.IME_ACTION_SEARCH 效果: 
  5. actionSend 发送,对应常量EditorInfo.IME_ACTION_SEND 效果:
  6. actionNext 下一个,对应常量EditorInfo.IME_ACTION_NEXT 效果:
  7. actionDone 完成,对应常量EditorInfo.IME_ACTION_DONE 效果:

 下面已搜索为例,演示一个实例,修改main.xml如下:

Xml代码  收藏代码
  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>  

  修改HelloEditText如下:

Java代码  收藏代码
  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. }  

 运行程序,点击回车(也就是搜索图标软键盘按钮)会显示该actionId.我们上面的每一个设置都会对应一个常量,这里的actionId就是那个常量值。

原文地址:https://www.cnblogs.com/lianghui66/p/3186114.html