让改变输入法回车键的图标

我们希望让输入法在给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;
} }

 

参考自:

http://spencer-dev.lofter.com/post/d7b9e_613758d

posted @ 2015-05-19 15:37  developer_Kale  阅读(1001)  评论(0编辑  收藏  举报
网站流量统计工具