使用SearchView的Android abs,onQueryTextListener无效

问题描述 投票:3回答:2

我正在尝试使用带有操作栏sherlock的SearchView支持v4版本。

所以我在操作栏中有我的搜索按钮 - >当我触摸它时键盘显示和搜索栏也是如此。

我的问题是我需要使用onQueryTextSubmit和onQueryTextChange上的侦听器,但它们永远不会被触发。我需要使用searh查询字符串并使用它做自定义的东西。

这是完整的activity.java

public class ActivityMain extends SherlockFragmentActivity implements OnQueryTextListener, DialogFragmentListener {
    /**
     * PRIVATE ATTRIBUTES
     */
    private static final String TAG                             = "ActivityMain";
    private ViewPager           _viewPager;
    private TabsAdapter         _tabsAdapter;
    private DialogFiltre        _dialogFiltre;
    private String              _searchCurrentQuery;
    // data
    private boolean             _doubleBackToExitPressedOnce    = false;


    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {

        menu.clear();
        switch ((int) _viewPager.getCurrentItem()) {
            case 0:
                getSupportMenuInflater().inflate(R.menu.empty_menu, menu);
                break;
            case 1:
                getSupportMenuInflater().inflate(R.menu.action_bar_menu, menu);
                break;
            case 2:
                getSupportMenuInflater().inflate(R.menu.empty_menu, menu);
                break;
        }
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getSupportMenuInflater().inflate(R.menu.action_bar_menu, menu);
        MenuItem searchItem = menu.findItem(R.id.search);
        SearchView searchView = (SearchView) searchItem.getActionView();
        searchView.setSubmitButtonEnabled(true);
        searchView.setOnQueryTextListener(queryTextListener);
        return true;
    }

    @Override
    public boolean onQueryTextSubmit(String query) {

        Log.i(TAG, "onQueryTextSubmit--");
        onSearchClicked(query);
        // hide keyboard
        InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {

        Log.d(TAG, "onQueryTextChange--");
        _searchCurrentQuery = newText.toString();
        EtablissementApplication._adapter.getFilter().filter(_searchCurrentQuery);
        return true;
    }

    private void onSearchClicked(String query) {

        Log.d(TAG, "onSearchClicked--");
        _searchCurrentQuery = query.toString();
        EtablissementApplication._adapter.getFilter().filter(_searchCurrentQuery);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case android.R.id.home:
                getSupportActionBar().setDisplayHomeAsUpEnabled(false);
                break;
            case R.id.search:
                break;
            case R.id.menu_filtre:
                _dialogFiltre = DialogFiltre.newInstance(R.string.menu_filtre, this);
                _dialogFiltre.setValidDialogListener(this);
                _dialogFiltre.show(getSupportFragmentManager(), null);
                break;
        }
        return super.onOptionsItemSelected(item);
    }
java android actionbarsherlock searchview searchable
2个回答
10
投票

您正在尝试使用新的SearchView,而不是使用SupportMenuInflater创建的SearchView。您将侦听器设置为您在屏幕上看到的不同SearchView。此外,每次调用onPrepareOptionsMenu时,都会创建新的SearchView,因此它没有设置侦听器。

尝试像这样做onPrepareOptionsMenu:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {

    menu.clear();
    switch ((int) _viewPager.getCurrentItem()) {
        case 0:
            getSupportMenuInflater().inflate(R.menu.empty_menu, menu);
            break;
        case 1:
            getSupportMenuInflater().inflate(R.menu.action_bar_menu, menu);
            MenuItem searchItem = menu.findItem(R.id.search);
            SearchView searchView = (SearchView) searchItem.getActionView();
            searchView.setSubmitButtonEnabled(true);
            searchView.setOnQueryTextListener(queryTextListener);
            break;
        case 2:
            getSupportMenuInflater().inflate(R.menu.empty_menu, menu);
            break;
    }
    return super.onPrepareOptionsMenu(menu);
}

并删除重写的方法

@Override
public boolean onCreateOptionsMenu(Menu menu) { }

0
投票

您可能必须在SearchView参考上设置searchView.setOnQueryTextListener(this);

© www.soinside.com 2019 - 2024. All rights reserved.