如何在searchView中收听键盘搜索按钮

时间:2014-10-10 19:27:26

标签: android android-softkeyboard searchview

我有一个SearchView。当用户点击键盘搜索按钮时,我需要拨打服务器电话。听众的代码是什么样的?我想我必须使用OnClickListener。但是知道它的内部代码是搜索按钮,我不知道如何确定它。

4 个答案:

答案 0 :(得分:62)

我这样做了

onQueryTextSubmit是您要查找的方法。

在搜索视图上设置setOnQueryTextListener

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);

    MenuItem searchItem = menu.findItem(R.id.search_city);
    searchView = (SearchView) searchItem.getActionView();
    searchView.setQueryHint("Search View Hint");

    searchView.setOnQueryTextListener(new OnQueryTextListener() {

        @Override
        public boolean onQueryTextChange(String newText) {
            //Log.e("onQueryTextChange", "called");
            return false;
        }

        @Override
        public boolean onQueryTextSubmit(String query) {


            // Do your task here

            return false;
        }

    });

    return true;
}

希望这个帮助

答案 1 :(得分:1)

这是kotlin版本:

        searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {

            override fun onQueryTextChange(s: String): Boolean {
                // make a server call
                return true
            }

            override fun onQueryTextSubmit(s: String): Boolean {
                val intent = Intent(applicationContext, YourSearchableActivity::class.java)
                intent.putExtra(SearchManager.QUERY, s)
                intent.putExtra(CUSTOM_MESSAGE, "you can also add custom message when submitting the search action")
                intent.setAction(Intent.ACTION_SEARCH)
                startActivity(intent)
                return true
            }
        })

答案 2 :(得分:1)

您可以在这里找到Android的最佳答案,而不是Kotlin

        binding.edtSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                // Your piece of code on keyboard search click
                if (AppValidator.isValid(SearchEventsActivity.this, binding.edtSearch, "Please enter your search keyword.")) {
                    searchKeyword = binding.edtSearch.getText().toString().trim();
                    //Search Events categories
                    callSearchEventsApi();
                }

                return true;
            }
            return false;
        }
    });

确保已在EditText中添加以下行

android:imeOptions="actionSearch"

就像下面这样:-

 <EditText
                android:id="@+id/edtSearch"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginLeft="@dimen/_15dp"
                android:background="@null"
                android:fontFamily="@font/roboto_regular"
                android:textColorHint="@color/text_color_light"
                android:layout_gravity="center"
                android:textSize="@dimen/_16sp"
                android:maxLines="1"
                android:singleLine="true"
                android:imeOptions="actionSearch"
                android:hint="@string/search"/>

我希望这可以帮助您解决问题。!!!!

答案 3 :(得分:0)

SearchView方法

  

setOnQueryTextLister

将按以下方式完成您的工作。

<table>
  <thead>
  </thead>
  <tbody>
    <tr class="compDetails-row">
      <td>Row1 Column1</td>
      <td>Row1 Column2</td>
      <td>Row1 Column3</td>
    </tr>
    <tr class="compDetails-row">
      <td>Row2 Column1</td>
      <td>Row2 Column2</td>
      <td>Row2 Column3</td>
    </tr>
    <tr>
      <td>View Details</td>
    </tr>
  </tbody>
  <tbody>
    <tr class="compDetails-row">
      <td>Row1 Column1</td>
      <td>Row1 Column2</td>
      <td>Row1 Column3</td>
    </tr>
    <tr>
      <td>View Details</td>
    </tr>
  </tbody>
</table>

此处searchView是SearchView的对象。