与福尔摩斯动作条搜索查看setOnKeyListener搜索 [英] search with Sherlock ActionBar SearchView setOnKeyListener

查看:157
本文介绍了与福尔摩斯动作条搜索查看setOnKeyListener搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想的ActionBarSherlock搜索查看。
不幸的是,

 公共布尔安其(查看为arg0,ARG1 INT,KeyEvent的事件)

不被解雇。
你知道原因吗?
我从这篇文章看
<一href=\"http://stackoverflow.com/questions/13527181/searchview-imeoptions-and-onquerytextsubmit-support\">SearchView imeOptions和onQueryTextSubmit支持
使得用户在另一种方法解决了这个问题。也许我也应该这样做?

感谢

下面是我的code:

  @覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    //用来放暗图标光操作栏
    布尔isLight = SampleList.THEME == R.style.Theme_Sherlock_Light;    //创建搜索视图
   最终搜索查看搜索查看=新的搜索查看(getSupportActionBar()getThemedContext());
    sea​​rchView.setQueryHint(搜索国家a€|);    的System.out.println(searchView.getQuery()+ searchView.getQuery());    sea​​rchView.setOnKeyListener(新OnKeyListener()
    {
    / **
      *此侦听用户preSS上的ENTER键
      *键盘然后隐藏虚拟键盘
      * /
           @覆盖
        公共布尔安其(查看为arg0,ARG1 INT,KeyEvent的事件){
        //如果事件上的Enter键一键按下事件               的System.out.println(---&gt;中+ searchView.getQuery());
        如果((event.getAction()== KeyEvent.ACTION_DOWN)及&放大器;
        (ARG1 == KeyEvent.KEY code_ENTER))
        {
        InputMethodManager IMM =(InputMethodManager)SearchViews.this.getSystemService(SearchViews.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(searchView.getWindowToken(),0);
        返回true;
        }
        返回false;
        }    });    menu.add(搜索)
        .setIcon(?isLight R.drawable.ic_search_inverse:R.drawable.abs__ic_search)
        .setActionView(搜索查看)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);    返回true;
}


解决方案

避免做这样的事。使用 SearchView.OnQueryTextListener

 最终搜索查看搜索查看=新的搜索查看(getSupportActionBar()getThemedContext());
sea​​rchView.setQueryHint(搜索国家..);
sea​​rchView.setOnQueryTextListener(新SearchView.OnQueryTextListener(){    @覆盖
    公共布尔onQueryTextChange(字符串newText){
        返回false;
    }
    @覆盖
    公共布尔onQueryTextSubmit(查询字符串){
        如果(query.length()!= 0){
            的System.out.println(---&gt;中+查询);
            //这里手柄搜索
            返回true;
        }
        返回false;
    }
});

务必阅读的文档

I am trying the ActionBarSherlock SearchView. unfortunately the

       public boolean onKey(View arg0, int arg1, KeyEvent event)

is not fired. Do you know the reason? I see from this post SearchView imeOptions and onQueryTextSubmit support that a user solved the problem in another way. Maybe I should do the same?

Thanks

Here is my code:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    //Used to put dark icons on light action bar
    boolean isLight = SampleList.THEME == R.style.Theme_Sherlock_Light;

    //Create the search view
   final SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
    searchView.setQueryHint("Search for countries…");

    System.out.println("searchView.getQuery()"+searchView.getQuery());

    searchView.setOnKeyListener(new OnKeyListener()
    {
    /**
      * This listens for the user to press the enter button on
      * the keyboard and then hides the virtual keyboard
      */
           @Override
        public boolean onKey(View arg0, int arg1, KeyEvent event) {
        // If the event is a key-down event on the "enter" button

               System.out.println("--->"+searchView.getQuery());
        if ( (event.getAction() == KeyEvent.ACTION_DOWN ) &&
        (arg1 == KeyEvent.KEYCODE_ENTER) )
        {
        InputMethodManager imm = (InputMethodManager)     SearchViews.this.getSystemService(SearchViews.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
        return true;
        }
        return false;
        }



    } );

    menu.add("Search")
        .setIcon(isLight ? R.drawable.ic_search_inverse : R.drawable.abs__ic_search)
        .setActionView(searchView)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

    return true;
}

解决方案

Avoid doing anything like this. Use SearchView.OnQueryTextListener

final SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
searchView.setQueryHint("Search for countries..");
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

    @Override
    public boolean onQueryTextChange(String newText) {
        return false;
    }


    @Override
    public boolean onQueryTextSubmit(String query) {
        if (query.length() != 0) {
            System.out.println("--->" + query);
            // handle search here
            return true;
        }
        return false;
    }
});

Always read the docs

这篇关于与福尔摩斯动作条搜索查看setOnKeyListener搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆