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

相关文章:

  • 2021-10-07
  • 2021-07-13
  • 2022-01-09
  • 2022-02-01
  • 2022-12-23
  • 2021-08-11
  • 2022-01-23
  • 2021-09-21
猜你喜欢
  • 2022-12-23
  • 2021-12-10
  • 2022-12-23
  • 2021-08-22
  • 2021-07-11
  • 2022-01-09
  • 2022-12-23
相关资源
相似解决方案