我們希望讓輸入法在給EditText輸入文字的時候,右下角有一個搜索的按鈕圖標,這就需要在java代碼和xml中做點設置了。

一、xml
<EditText android:id="@+id/searchEditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:imeOptions="actionSearch" android:inputType="text" android:singleLine="true" />二、java
EditText editText = (EditText) findViewById(R.id.searchEditText); // http://spencer-dev.lofter.com/post/d7b9e_613758d editText.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_SEARCH) { // 這里的action和在layout中設置的android:imeOptions屬性是對應的. // 這個方法的作用就是,動作之行后的回調,在用戶輸入完成后,點擊了輸入法中的搜索按鈕,就會執行這個方法 // 返回值: 如果你處理了該事件,返回true;否則返回false。 //TODO:這時你要在這里執行真正的搜索操作 Toast.makeText(MainActivity.this, "search", Toast.LENGTH_SHORT).show(); } return true;} }新聞熱點
疑難解答