標籤:android edittext imeoptions
在我們的手機中,雖然通常IME軟鍵盤右下角會是斷行符號按鍵,但我們經常會看到點擊不同的編輯框,IME軟鍵盤右下角會有不同的表徵圖。例如:
點擊瀏覽器網址欄的時候,IME軟鍵盤右下角會變成“GO”或“前往;
而我們點擊Google搜尋方塊,IME軟鍵盤右下角會變成 放大鏡 或者“搜尋”。
而決定這個表徵圖的變換的參數就是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>
Android之EditText imeOptions屬性解析