【问题标题】:how to listen to keyboard search button in searchView如何在 searchView 中收听键盘搜索按钮
【发布时间】:2014-12-06 01:17:45
【问题描述】:

我有一个 SearchView。当用户点击 keyboard 搜索按钮时,我需要进行服务器调用。监听器的代码是什么样的?我想我必须使用 OnClickListener。但是知道它是搜索按钮的内部代码,我不确定如何确定。

【问题讨论】:

  • 它发送 KeyEvent#KEYCODE_SEARCH 键,因此您可以使用 onKeyEventListener 或活动的 dispatchKeyEvent 查找它
  • @DeeV 文档说 onKeyEventListener 用于硬件密钥。如果它同样适用于软键盘,为什么还要专门说硬件?无论如何,我会尝试一下,因为你说这是答案。
  • @DeeV 在提出问题之前我已经尝试过 onEditorActionListener 。但是 SearchView 无法识别它。
  • 显示搜索视图的一些代码
  • @QadirHussain 你明白这个问题吗?您的全面评论不属于这里。

标签: android android-softkeyboard searchview


【解决方案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"/>

我希望这能帮助你解决你的问题!!!!

【讨论】:

    【解决方案2】:

    这是 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
                }
            })
    

    【讨论】:

      【解决方案3】:

      SearchView 方法

      setOnQueryTextLister

      将按如下所示完成您的工作。

      searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                  @Override
                  public boolean onQueryTextSubmit(String query) {
                      showSnackBar(query.toString());
                      return false;
                  }
      
                  @Override
                  public boolean onQueryTextChange(String newText) {
                      return false;
                  }
              });
      

      这里的searchView是SearchView的对象。

      【讨论】:

        【解决方案4】:

        我已经这样做了

        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;
        }
        

        希望有帮助

        【讨论】:

        • 耶!好吧,你打败了我的回应。我会投票!
        猜你喜欢
        • 2020-05-04
        • 1970-01-01
        • 1970-01-01
        • 2021-12-12
        • 2023-04-03
        • 2013-01-31
        • 2013-07-11
        • 1970-01-01
        相关资源
        最近更新 更多