searchview无法获得焦点但键盘打开

时间:2016-08-09 14:18:22

标签: android searchview android-searchmanager

我使用searchview进行搜索功能我的问题是,当我点击搜索图标键盘被打开但搜索视图没有获得焦点下面是我的代码

menu.xml文件

 <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
        <item
            android:id="@+id/send_invitation"
            android:icon="@drawable/send_icon_invite_participant"
            android:orderInCategory="93"
            android:title="@string/invitation"
            app:showAsAction="always">
        </item>

        <item
            android:id="@+id/search"
            android:orderInCategory="100"
            app:actionViewClass="android.support.v7.widget.SearchView"
            android:icon="@drawable/search_icn"
            android:title="@string/search"
            app:showAsAction="always"/>

</menu>

下面是方法

的代码
@Override
public void onCreateOptionsMenu(final Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.invite_participant, menu);
    final MenuItem searchItem = menu.findItem(R.id.search);
    SearchManager searchManager = (SearchManager) activity.getSystemService(Context.SEARCH_SERVICE);
    final SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
    EditText searchEditText = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
    searchEditText.setTextColor(getResources().getColor(R.color.colorAccent));
    searchEditText.setHintTextColor(getResources().getColor(R.color.colorAccent));
    searchItem.expandActionView();
    ImageView searchIcon = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_button);
    searchIcon.setImageResource(R.drawable.search_icn);
    ImageView searchCloseIcon = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
    searchCloseIcon.setImageResource(R.drawable.reject_icon_notification);
    if (searchView != null) {
        searchView.setSearchableInfo(searchManager.getSearchableInfo(activity.getComponentName()));
        searchView.setOnSearchClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setItemsVisibility(menu, searchItem, false);
            }
        });
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                searchView.clearFocus();
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                if (adapter != null) {
                    adapter.fliter(newText);
                }
                return false;
            }
        });
        searchView.setOnCloseListener(new SearchView.OnCloseListener() {
            @Override
            public boolean onClose() {
                setItemsVisibility(menu, searchItem, true);
                return false;
            }
        });
    }
}

private void setItemsVisibility(Menu menu, MenuItem exception, boolean visible) {
    for (int i=0; i<menu.size(); ++i) {
        MenuItem item = menu.getItem(i);
        if (item != exception) item.setVisible(visible);
    }
}

我无法找到为什么它没有集中注意力请帮助我找到我做错了任何帮助。

2 个答案:

答案 0 :(得分:1)

您可以通过在Activity的 int daysUntilBill = (billingDay >= today.Day) ? billingDay - today.Day : billingDay + DateTime.DaysInMonth(today.Year, today.Month) - today.Day; 方法中添加以下代码来实现它。

onCreate

或在AndroidManifest文件中添加此行

getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

修改

尝试这样的事情

android:windowSoftInputMode="stateHidden"

答案 1 :(得分:1)

您可以使用searchview添加这些线条以进行对焦。

 searchView.setFocusable(true);
    searchView.requestFocus();
    searchView.requestFocusFromTouch();

如果它不起作用,则首先展开搜索视图,然后请求焦点。