如何在操作栏中关闭搜索时执行单击操作

时间:2013-11-15 06:57:24

标签: android search

这里我想在点击'<'时隐藏搜索+按钮按钮如下图所示 点击它时如何采取行动是否有@override方法? enter image description here

这是我的代码:

private MenuItem item2;
item2 = menu1.getItem(0); // get position of item
item2.setOnActionExpandListener(new OnActionExpandListener() { // perform on click action here

    @Override
    public boolean onMenuItemActionExpand(MenuItem item) {
        // TODO Auto-generated method stub
        item2 = menu1.getItem(2).setVisible(false); // hide button
        item2 = menu1.getItem(1).setVisible(true); // enable button
        return true;
    }

    @Override
    public boolean onMenuItemActionCollapse(MenuItem item) {
        // TODO Auto-generated method stub
        item2 = menu1.getItem(1).setVisible(false); // hide button
        item2 = menu1.getItem(2).setVisible(true); // enable button
        return true;
    }
});

2 个答案:

答案 0 :(得分:1)

我使用此代码解决了它:

@Override
public boolean onOptionsItemSelected(final MenuItem item) {
private MenuItem item2;
if (item.getTitle().equals("Search")) {
item2 = menu1.getItem(0); // get position of item
            item2.setOnActionExpandListener(new OnActionExpandListener() { // perform on click action here

                @Override
                public boolean onMenuItemActionExpand(MenuItem item) {
                    // TODO Auto-generated method stub
                    item2 = menu1.getItem(2).setVisible(false); // hide button
                    item2 = menu1.getItem(1).setVisible(true); // enable button
                    return true;
                }

                @Override
                public boolean onMenuItemActionCollapse(MenuItem item) {
                    // TODO Auto-generated method stub
                    item2 = menu1.getItem(1).setVisible(false); // hide button
                    item2 = menu1.getItem(2).setVisible(true); // enable button
                    return true;
                }
            });
       }
}

答案 1 :(得分:0)

我相信这就是你所需要的:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        // hide your icon
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

编辑:假设您正在使用Android的Action Bar实施