咱們但願讓輸入法在給EditText輸入文字的時候,右下角有一個搜索的按鈕圖標,這就須要在java代碼和xml中作點設置了。java
1、xmlandroid
<EditText android:id="@+id/searchEditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:imeOptions="actionSearch" android:inputType="text" android:singleLine="true" />
2、javaide
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; } }