EditText中imeOptions属性设置无效时解决方法

来源:互联网 发布:软件精灵下载安装 编辑:程序博客网 时间:2024/05/21 17:01

虽然通常输入法软键盘右下角会是回车按键

但我们经常会看到点击不同的编辑框,输入法软键盘右下角会有不同的图标

点击浏览器网址栏的时候,输入法软键盘右下角会变成“GO”或“前往”

而我们点击Google搜索框,输入法软键盘右下角会变成 放大镜 或者“搜索”

而决定这个图标的变换的参数就是EditText中的 Android:imeOptions

android:imeOptions的值有actionGo、 actionSend 、actionSearch、actionDone等,这些意思都很明显

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <EditText  
  2.       android:id="@+id/editText"  
  3.       android:layout_width="200dp"  
  4.       android:layout_height="wrap_content"  
  5.       android:imeOptions="actionSearch"  
  6.    />  

而其在Java代码中对应的值为EditorInfo.IME_ACTION_XXX 

在代码中通过editText.setOnEditorActionListener方法添加相应的监听,因为有些action是需要在代码中添加具体的相关操作的

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. EditText editText = (EditText) contentView.findViewById(R.id.editText);  
  2.         editText.setOnEditorActionListener(new OnEditorActionListener() {  
  3.             @Override  
  4.             public boolean onEditorAction(TextView v, int actionId,  
  5.                     KeyEvent event) {  
  6.                 if (actionId == EditorInfo.IME_ACTION_SEARCH) {  
  7.                     Toast.makeText(getActivity(), "1111111",Toast.LENGTH_SHORT).show();  
  8.                 }  
  9.   
  10.                 return false;  
  11.             }  
  12.         });  
然而当我们设置这一切后,却发现点击输入框,输入法键盘完全没变化,还是回车键

这并不是上面的属性和方法无效,而是我们还需要设置别的属性来使它们生效

经过试验 设置下面两个属性中的一个即可使这个属性生效(应该还有其他的属性也可以,没去试验)

1 将singleLine设置为true

2 将inputType设置为text 

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <EditText  
  2.       android:id="@+id/editText"  
  3.       android:layout_width="200dp"  
  4.       android:layout_height="wrap_content"  
  5.       android:imeOptions="actionSearch"  
  6.       android:singleLine="true"  
  7.       android:inputType="text"  
  8.    />  

java代码设置

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. editText.setImeOptions(EditorInfo.IME_ACTION_SEARCH);  
  2. editText.setInputType(EditorInfo.TYPE_CLASS_TEXT);  
  3. editText.setSingleLine(true);  
0 0