如何以编程方式打开搜索建议列表?

问题描述 投票:0回答:3

我的Android应用程序中有一个SearchView,它可以显示给定输入的建议。现在我想要以下行为:如果我完成键盘输入并按下软键盘上的“输入”按钮,那么我希望键盘消失,但带有搜索建议的列表应该保留。现在的行为是,如果我输入一些字母,建议就会出现(好!)但是如果我完成并按回车键,列表就会消失(不好!)。那么如何重新打开列表但隐藏键盘呢?

通常,会触发意图ACTION_SEARCH,并以新活动打开并显示搜索结果的方式处理。我只希望打开带有建议的列表。

一些源代码:

在AddTeam类的onCreate()中:

    SearchView searchView= (SearchView) findViewById(R.id.searchView);
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){

        @Override
        public boolean onQueryTextSubmit(String s) {
            InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            return false;
        }

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


    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

意图处理:

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    handleIntent(intent);
}

private void handleIntent(Intent intent) {
    if (Intent.ACTION_VIEW.equals(intent.getAction())) {
        // handles a click on a search suggestion; launches activity to show word
        Uri uri = intent.getData();
        Cursor cursor = managedQuery(uri, null, null, null, null);

        if (cursor == null) {
            finish();
        } else {
            cursor.moveToFirst();
            doMoreCode();
        }
    } else if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        // handles a search query
        String query = intent.getStringExtra(SearchManager.QUERY);
        //////////////////////////////////////////////////////
        //WANTED FEATURE!
        OpenSearchSuggestionsList();
        //WANTED FEATURE!
        //////////////////////////////////////////////////////



    }
}

表现:

    <activity
        android:name=".AddTeam"
        android:configChanges="keyboard|screenSize|orientation"
        android:label="@string/title_activity_teamchooser"
        android:launchMode="singleTop" >

        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
    </activity>

检索:

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
        android:label="@string/title_activity_settings"
        android:hint="@string/search_hint"
        android:searchSettingsDescription="Enter Word"
        android:searchSuggestAuthority="com.mypackage.DataProvider"
        android:searchSuggestIntentAction="android.intent.action.VIEW"
        android:searchSuggestIntentData="content://com.mypackage.DataProvider/teamdaten"
        android:searchSuggestSelection=" ?"
        android:searchSuggestThreshold="1"
        android:includeInGlobalSearch="true"
        >
 </searchable>

AddTeam Layout xml的一部分:

<android.support.v7.widget.SearchView
    android:id="@+id/searchView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:textColor="#000000"
    volleyballinfo:iconifiedByDefault="false"
    volleyballinfo:queryHint="@string/search_hint"/>
android searchview search-suggestion
3个回答
2
投票

注意:我的答案仅基于源代码检查,可能不会产生积极/有利的结果。此外,下面的代码是从内存中写入的 - 它可能包含拼写错误。

好。在内部,SearchView使用自定义AutoCompleteTextView来显示建议。这个自定义AutoCompleteTextView通过多个渠道监听提交事件:View.OnKeyListener,覆盖SearchView#onKeyDown(int, KeyEvent)OnEditorActionListener

在提交事件(按下ENTER键时 - 在您的情况下),使用SearchView#dismissSuggestions解除建议弹出:

private void dismissSuggestions() {
    mSearchSrcTextView.dismissDropDown();
}

如你所见,SearchView#dismissSuggestions()打电话给AutoCompleteTextView#dismissDropDown()。所以,为了显示下拉列表,我们应该能够调用AutoCompleteTextView#showDropDown()

但是,AutoCompleteTextView使用的自定义SearchView实例是私有的,没有定义访问器。在这种情况下,我们可以尝试找到这个View,将其投射到AutoCompleteTextView,并在其上调用showDropDown()

....
// Your snippet
else if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
    // handles a search query
    String query = intent.getStringExtra(SearchManager.QUERY);
    OpenSearchSuggestionsList(searchView);
}
....

// Edited
// This method will accept the `SearchView` and traverse through 
// all of its children. If an `AutoCompleteTextView` is found,
// `showDropDown()` will be called on it.
private void OpenSearchSuggestionsList(ViewGroup viewGroup) {
    for (int i = 0; i < viewGroup.getChildCount(); i++) {
        View child = viewGroup.getChildAt(i);

        if (child instanceof ViewGroup) {
            OpenSearchSuggestionsList((ViewGroup)child);
        } else if (child instanceof AutoCompleteTextView) {
            // Found the right child - show dropdown
            ((AutoCompleteTextView)child).showDropDown();
            break; // We're done
        }
    }
}

期待您对此发表评论。


0
投票

在搜索视图中有一个名为onQueryTextSubmit的覆盖方法,您可以先尝试从此方法返回false。

如果上述方法不起作用,请尝试此操作,

@Override
  public boolean onQueryTextSubmit(String query) {

      InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
      inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
      return false;
  }

希望这可以帮助 :)


0
投票

这将有所帮助,注意使用应用程序appcompat库中的R导入android.support.v7.appcompat.R

mQueryTextView = (AutoCompleteTextView) searchView.findViewById(R.id.search_src_text);

mQueryTextView.showDropDown()

0
投票

接受的答案是一个很好的答案,并引导我得到另一个可能使其他人受益的简单答案:

private void OpenSearchSuggestionsList() {
    int autoCompleteTextViewID = getResources().getIdentifier("search_src_text", "id", getPackageName());
    AutoCompleteTextView  searchAutoCompleteTextView = searchView.findViewById(autoCompleteTextViewID);
    searchAutoCompleteTextView.showDropDown();
}
© www.soinside.com 2019 - 2024. All rights reserved.