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

时间:2015-11-21 19:21:16

标签: android searchview search-suggestion

我的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"/>

4 个答案:

答案 0 :(得分: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
        }
    }
}

期待您的评论。

答案 1 :(得分:0)

在搜索视图中有一个名为ALTER PROCEDURE [dbo].[usr_INSERTRECORD] -- Add the parameters for the stored procedure here @SourceTable SYSNAME, @TableName SYSNAME, @TransDate Date, @Symbol nvarchar(50) --<@Param2, sysname, @p2> <Datatype_For_Param2, , int> = <Default_Value_For_Param2, , 0> AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; -- Insert statements for procedure here DECLARE @DATEVARCHAR nvarchar(4000); SET @DATEVARCHAR = CONVERT(NVARCHAR, @TransDate, 103); -- Actual Statement that does the insert -- This is the statement causing the issue; works if schema matches on both tables DECLARE @SQLCommand NVARCHAR(MAX) = N'INSERT INTO ' + QUOTENAME(@TableName) + ' SELECT * FROM ' + + QUOTENAME(@SourceTable) + ' WHERE (TRANSDATE = @TransDate AND SYMBOL = @SYMBOL)'; EXECUTE [dbo].[sp_executesql] @sqlCommand, N'@TransDate date, @Symbol nvarchar(50)', @TransDate = @TransDate, @Symbol = @Symbol; END 的覆盖方法,您可以先尝试从此方法返回false。

如果上述方法无效,请尝试此操作,

onQueryTextSubmit

希望这会有所帮助:)

答案 2 :(得分:0)

这会有所帮助,请注意使用app appcompat库中的R 导入android.support.v7.appcompat.R

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

mQueryTextView.showDropDown()

答案 3 :(得分:0)

被接受的答案是一个很好的答案,它使我想到另一个可能使他人受益的简单答案:

private void OpenSearchSuggestionsList() {
    int autoCompleteTextViewID = getResources().getIdentifier("search_src_text", "id", getPackageName());
    AutoCompleteTextView  searchAutoCompleteTextView = searchView.findViewById(autoCompleteTextViewID);
    searchAutoCompleteTextView.showDropDown();
}