OnQueryTextListenerCompat not getting triggered

纵然是瞬间 提交于 2019-12-25 05:13:39

问题


I'm trying to make an app that uses SearchView compatible with devices running Android version 2.3 up to 4.4+. I currently have the following code inside an ActionBarActivity, which works fine in devices running Honeycomb and above but not earlier devices:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    android.support.v7.widget.SearchView searchView = (android.support.v7.widget.SearchView) menu.findItem(R.id.action_search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setIconified(true);
    searchView.setOnQueryTextListener(new OnQueryTextListener() {

        @Override
        public boolean onQueryTextSubmit(String s) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String s) {
            return false;
        }
    });
} else {
    View searchView = SearchViewCompat.newSearchView(this);
    SearchViewCompat.setOnQueryTextListener(searchView, new OnQueryTextListenerCompat() {
        @Override
        public boolean onQueryTextSubmit(String s) {
            return false;
        }

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

The problem I'm facing is that the SearchView UI shows up fine in earlier devices, but the OnQueryTextListenerCompat doesn't seem to get triggered ever. Am I missing something? Thanks.


回答1:


I've solved this problem by not using SearchViewCompat I tested it's compatability from Gingerbread and to the latest version

in my search_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:yourapp="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" >
 <item
        android:id="@+id/menu_item_search"
        android:icon="@android:drawable/ic_menu_search"
        android:orderInCategory="1"
        android:title="@string/menu_search"
        yourapp:actionViewClass="android.support.v7.widget.SearchView"
        yourapp:showAsAction="always|collapseActionView"/>
</menu>

and in my Main.xml

    public class Main extends ActionBarActivity implements OnQueryTextListener {
    private SearchView searchView;
    private SearchManager searchManager;
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.activity_main);
    }


     @Override
            public boolean onCreateOptionsMenu(final Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.search_menu, menu);
                View view = MenuItemCompat.getActionView(menu.findItem(R.id.menu_item_filter)); 
    searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
                MenuItem searchItem = menu.findItem(R.id.menu_item_search);
                searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
                searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
                searchView.setQueryHint("Search...");
                searchView.setIconifiedByDefault(false); 
                searchView.setIconified(false);
                searchView.setOnQueryTextListener(Main.this);
                searchView.clearFocus();
                return super.onCreateOptionsMenu(menu);
    }
    public boolean onQueryTextChange(String val) {

        if(!val.isEmpty()){
            Log.e(""+val);
        }
        return false;
    }

    @Override
    public boolean onQueryTextSubmit(String val) {  
        Log.e(""+val);
        return false;
    }
}


来源:https://stackoverflow.com/questions/25597139/onquerytextlistenercompat-not-getting-triggered

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!