【问题标题】:How do i know when the user tapped the search key?我怎么知道用户何时点击了搜索键?
【发布时间】:2015-04-30 08:16:03
【问题描述】:

我已经在我的应用程序中实现了关键字搜索,但是当我点击软键盘上的搜索(即问号)键时,我没有收到任何回调。我已经在我的 EditText 中添加了一个 TextWatcher,但是当我点击搜索时它不会被调用。 Activity.onKeyUp() 也没有被调用。

有什么方法可以知道用户何时点击软键盘上的搜索键?

提前谢谢...

【问题讨论】:

    标签: android android-softkeyboard


    【解决方案1】:

    像这样使用 searchView

     SearchView sv=(SearchView)findViewByid(R.id.sv);
          sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                    @Override
                    public boolean onQueryTextSubmit(String s) {
                        return false;
                         //here is when clicking the search button in keyboard
                    }
    
                    @Override
                    public boolean onQueryTextChange(String s) {
                       //here is when the text change
                        return false;
                    }
                });
    

    编辑我已经找到了这样做:

    在xml中

      <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText"  
        android:imeOptions="actionSearch"
        android:inputType="text"/>
    

    在 onCreate 方法中

     ((EditText)findViewById(R.id.editText)).setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    Toast.makeText(MainActivity.this,"searching",Toast.LENGTH_LONG).show();
                    return true;
                }
                return false;
            }
        });
    

    就是这样

    【讨论】:

    • 我没有使用 SearchView。我在操作栏中使用我自己的客户 EditText。还有其他方法吗?
    • 非常感谢您的更新。它调用 onEditorAction(),但我无法关闭键盘。关闭键盘以响应击键可能是不可能的,因为它认为用户仍在键入。 :-/
    • aaa 我明白了,您隐藏键盘的键盘按钮被替换为搜索按钮?这是你有的吗?
    • 现在可以使用了,谢谢。我的键盘问题与您的解决方案无关。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-15
    • 1970-01-01
    • 2015-12-31
    相关资源
    最近更新 更多