在android中使用查询启动搜索活动

时间:2016-01-19 07:17:15

标签: android android-search

我可以在android中创建SearchActivity。效果很好,但我想在Query开始时在Search Widget中显示SearchActivity。我怎么能得到这个? 这是我的MainActivity搜索查询plumber enter image description here

当我点击搜索按钮时,这是我SearchActivity enter image description here

现在我想要的是,当SearchActivity被打开时,在输入查询时,plumber中的Search.Widget就像MainActivity一样{。}}。

1 个答案:

答案 0 :(得分:0)

这就是你要找的东西 menu_search.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".ui.home.activities.TransactionSearchActivity">
    <item
        android:id="@+id/action_search"
        android:icon="@android:drawable/ic_menu_search"
        android:title="@string/search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="ifRoom" />
</menu>

private EditText searchPlate;
    @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.menu_search, menu);
            final MenuItem searchItem = menu.findItem(R.id.action_search);

            if (searchItem != null) {
                searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
                searchView.setOnCloseListener(new SearchView.OnCloseListener() {
                    @Override
                    public boolean onClose() {
                        return false;
                    }
                });
                searchView.setOnSearchClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                    }
                });
                searchPlate = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
                searchPlate.setHint("Search");
                View searchPlateView = searchView.findViewById(android.support.v7.appcompat.R.id.search_plate);
                searchPlateView.setBackgroundColor(ContextCompat.getColor(this, android.R.color.transparent));
                // use this method for search process
                searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                    @Override
                    public boolean onQueryTextSubmit(String query) {
                        // use this method when query submitted
                        return false;
                    }

                    @Override
                    public boolean onQueryTextChange(String newText) {
                        // use this method for auto complete search process
                        return false;
                    }
                });
                SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
                searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

            }
            return super.onCreateOptionsMenu(menu);
        }

然后在onResume()

使用此

searchPlate.setText("My Search Query");