使用SearchView的Android abs,onQueryTextListener不起作用

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

所以我有我的搜索按钮在操作栏 - >当我触摸它的键盘显示和searchBar了。

我的问题是,我需要使用侦听器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);
    }

您正尝试使用新的SearchView,而不是使用SupportMenuInflater创建的那个。 您将侦听器设置为您在屏幕上看到的不同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) { }
链接地址: http://www.djcxy.com/p/92955.html

上一篇: Android abs with SearchView, onQueryTextListener not working

下一篇: Android SearchView empty string