在我们的手机中,虽然通常输入法软键盘右下角会是回车按键,但我们经常会看到点击不同的编辑框,输入法软键盘右下角会有不同的图标。例如:
点击浏览器网址栏的时候,输入法软键盘右下角会变成“GO”或“前往;
而我们点击Google搜索框,输入法软键盘右下角会变成 放大镜 或者“搜索”。
而决定这个图标的变换的参数就是EditText中的 android:imeOptions属性。android:imeOptions的值有actionGo、 actionSend 、actionSearch、actionDone等
设置 android:imeOptions=”actionDone” ,软键盘下方变成“完成”,点击后光标保持在原来的输入框上,并且软键盘关闭。
android:imeOptions=”actionSend” 软键盘下方变成“发送”,点击后光标移动下一个。
在这里设置的imeOptions如何使用呢?如下面的代码,让EditText实现OnEditorActionListener接口,在onEditorAction方法中actionId就对应我们设置的imeOptions。系统默认的actionId有:EditorInfo.IME_NULL、EditorInfo.IME_ACTION_SEND、EditorInfo.IME_ACTION_DONE等。这样我们就可以根据不同的EditText来实现不同的软键盘右下角功能键。
<code class="hljs java has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">IMEOptionsActivity</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">extends</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">Activity</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">implements</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">OnEditorActionListener</span> {</span> EditText etDone; EditText etEmail; EditText etNumber; <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">onCreate</span>(Bundle savedInstanceState) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">super</span>.onCreate(savedInstanceState); setContentView(R.layout.imf_layout); etDone= (EditText)findViewById(R.id.done_content); etEmail = (EditText)findViewById(R.id.email_content); etNumber = (EditText)findViewById(R.id.number_content); etDone.setOnEditorActionListener(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>); etEmail.setOnEditorActionListener(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>); etNumber.setOnEditorActionListener(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>); } <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">boolean</span> <span class="hljs-title" style="box-sizing: border-box;">onEditorAction</span>(TextView v, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> actionId, KeyEvent event) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">switch</span>(actionId){ <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">case</span> EditorInfo.IME_NULL: System.out.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Done_content: "</span> + v.getText() ); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">break</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">case</span> EditorInfo.IME_ACTION_SEND: System.out.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"send a email: "</span> + v.getText()); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">break</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">case</span> EditorInfo.IME_ACTION_DONE: System.out.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"action done for number_content: "</span> + v.getText()); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">break</span>; } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>; } } </code>
时间: 2024-11-05 18:37:27