2015-04-29 39 views
0

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

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

在此先感谢...

回答

1

使用搜索查看这样

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

这就是我没有使用搜索查看所有

+0

。我在操作栏中使用我自己的客户EditText。有另一种方法吗? –

+0

非常感谢您的更新。它调用onEditorAction(),但我无法关闭键盘。由于它认为用户仍在键入,因此可能无法关闭键盘来响应按键。 : -/ –

+0

为什么后退按钮不起作用? –

相关问题