OnQueryTextListener Compat没有被触发

时间:2014-08-31 23:19:02

标签: android searchview

我正在尝试制作一款使用SearchView与运行Android 2.3版本的设备兼容的应用程序,最高可达4.4+。我目前在ActionBarActivity中有以下代码,它在运行Honeycomb及以上但不是早期设备的设备中运行良好:

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;
        }
    });
}

我面临的问题是SearchView UI在早期的设备中显示得很好,但OnQueryTextListenerCompat似乎并没有被触发。我错过了什么吗?感谢。

1 个答案:

答案 0 :(得分:1)

我通过不使用SearchViewCompat解决了这个问题我测试了它与Gingerbread和最新版本的兼容性

在我的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>

并在我的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;
    }
}