本文介绍了搜索查看的OnCloseListener不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Android 3.0+动作条支持添加搜索查看,但我不能让 OnCloseListener 工作

下面是我的code:

  @覆盖
公共布尔onCreateOptionsMenu(功能菜单){
    。getMenuInflater()膨胀(R.menu.menu,菜单);
    搜索查看=(搜索查看)menu.findItem(R.id.search_textbox).getActionView();
    sea​​rchView.setOnQueryTextListener(新OnQueryTextListener(){
        @覆盖
        公共布尔onQueryTextChange(字符串newText){
            sea​​rchLibrary(newText);
            返回false;
        }
        @覆盖
        公共布尔onQueryTextSubmit(查询字符串){返回false; }
    });
    sea​​rchView.setOnCloseListener(新OnCloseListener(){
        @覆盖
        公共布尔的OnClose(){
            的System.out.println(测试1,2,3 ...。);
            返回false;
        }
    });
    返回true;
}
 

搜索的伟大工程,每一个正在工作除了 OnCloseListener 。没有什么是被打印到logcat的。这里的logcat的时候我是pressing的关闭按钮:

  02-17 13:01:52.914:I / TextType(446):TextType =为0x0
02-17 13:01:57.344:I / TextType(446):TextType =为0x0
02-17 13:02:02.944:I / TextType(446):TextType =为0x0
 

我已经通过的文档和样品​​,但似乎没有任何改变它。我对华硕的Transformer Prime和Galaxy Nexus的,无论在冰淇淋三明治运行它。任何想法?

更新:

是 - 的System.out.println()确实的工作。这里的证明:

  @覆盖
 公共布尔onQueryTextChange(字符串newText){
    的System.out.println(newText +你好);
    sea​​rchLibrary(newText);
    返回false;
 }
 

结果在这个logcat的:

  02-17 13:04:20.094:我/的System.out(21152):你好
02-17 13:04:24.914:我/的System.out(21152):thello
02-17 13:04:25.394:我/的System.out(21152):tehello
02-17 13:04:25.784:我/的System.out(21152):teshello
02-17 13:04:26.064:我/的System.out(21152):testhello
 

解决方案

我也遇到这个问题,我也没有办法,只好放弃oncloselistener。相反,你可以得到你的菜单项,然后 setOnActionExpandListener 。然后覆盖unimplents方法。

  @覆盖
公共布尔onMenuItemActionExpand(菜单项项){
    // TODO自动生成方法存根
    Log.d(*******,onMenuItemActionExpand);
    返回true;
}

@覆盖
公共布尔onMenuItemActionCollapse(菜单项项){
    //做你想要的时候关闭sesarchview
    //记得返回true;
    Log.d(*******,onMenuItemActionCollapse);
    返回true;
}
 

I'm trying to add support for the SearchView in the Android 3.0+ ActionBar, but I can't get the OnCloseListener to work.

Here's my code:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);
    searchView = (SearchView) menu.findItem(R.id.search_textbox).getActionView();
    searchView.setOnQueryTextListener(new OnQueryTextListener() {
        @Override
        public boolean onQueryTextChange(String newText) {
            searchLibrary(newText);
            return false;
        }
        @Override
        public boolean onQueryTextSubmit(String query) { return false; }
    });
    searchView.setOnCloseListener(new OnCloseListener() {
        @Override
        public boolean onClose() {
            System.out.println("Testing. 1, 2, 3...");
            return false;
        }
    });
    return true;
}

The search works great and every is working except for the OnCloseListener. Nothing is being printed to Logcat. Here's the Logcat for when I'm pressing the "Close" button:

02-17 13:01:52.914: I/TextType(446): TextType = 0x0
02-17 13:01:57.344: I/TextType(446): TextType = 0x0
02-17 13:02:02.944: I/TextType(446): TextType = 0x0

I've looked through the documentation and samples, but nothing seemed to change it. I'm running it on a Asus Transformer Prime and a Galaxy Nexus, both on Ice Cream Sandwich. Any ideas?

Update:

Yes - System.out.println() does work. Here's proof:

   @Override
 public boolean onQueryTextChange(String newText) {
    System.out.println(newText + "hello");
    searchLibrary(newText);
    return false;
 }

Results in this Logcat:

02-17 13:04:20.094: I/System.out(21152): hello
02-17 13:04:24.914: I/System.out(21152): thello
02-17 13:04:25.394: I/System.out(21152): tehello
02-17 13:04:25.784: I/System.out(21152): teshello
02-17 13:04:26.064: I/System.out(21152): testhello
解决方案

I also meet this problem, and I have no choice but give up "oncloselistener". Instead, you can get your menuItem, then setOnActionExpandListener. Then override unimplents methods.

@Override
public boolean onMenuItemActionExpand(MenuItem item) {
    // TODO Auto-generated method stub
    Log.d("*******","onMenuItemActionExpand");
    return true;
}

@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
    //do what you want to when close the sesarchview
    //remember to return true;
    Log.d("*******","onMenuItemActionCollapse");
    return true;
}

这篇关于搜索查看的OnCloseListener不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 04:55