0

再会!他在google 的文章上做了所有事情,但使用 ActionbarSherlock 及其实现 SearchView,但请求不会发送到 SearchActivity。你们中的任何人都可以知道可能是什么问题。只有当您按下键盘上的“输入”按钮时才会发送请求,我想让它类似于“实时搜索”,一旦开始输入,立即进行搜索。

AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
              package="ru.ex.Interface"
              android:versionCode="45"
              android:versionName="2.2.1b"
              android:installLocation="auto"
            >

        <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15"/>

        <uses-permission android:name="android.permission.INTERNET"/>
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>




        <application
                android:label="@string/app_name"
                android:icon="@drawable/ic_launcher"
                android:hardwareAccelerated="true"
                android:theme="@style/Theme.Sherlock.Light.Custom"

                >


            <!-- enable the search dialog to send searches to SearchableActivity -->
            <meta-data android:name="android.app.default_searchable"
                       android:value=".SearchActivity"/>

            <activity
                    android:name=".SearchActivity"
                    android:screenOrientation="portrait"
                    android:parentActivityName=".MainFragmentActivity"
                    android:launchMode="singleTop"
                    >
                <!-- This intent-filter identifies this activity as "searchable" -->

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

                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>

                <!-- This metadata entry provides further configuration details for searches -->
                <!-- that are handled by this activity. -->

                <meta-data
                        android:name="android.app.searchable"
                        android:resource="@xml/search_config" />

            </activity>

            <activity
                    android:name=".MainFragmentActivity"
                    android:screenOrientation="portrait"
                    android:launchMode="singleTask"
                    >

                <intent-filter>
                    <action android:name="android.intent.action.MAIN"/>
                    <category android:name="android.intent.category.LAUNCHER"/>
                </intent-filter>

            </activity>

        </application>


    </manifest> 

MainFragmentActivity 中的板条箱菜单

 @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {

        getSupportMenuInflater().inflate(R.menu.serch_menu, menu);            
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.search_menu).getActionView();            
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));            
        searchView.setIconifiedByDefault(false); 
        return true;
    } 

SearchActivity.java

public class SearchActivity extends CustomMenuFragmentActivity
{


    @Override
    public void onCreate(Bundle save)
    {
        super.onCreate(save);
        setContentView(R.layout.search_page);
        handleIntent(getIntent());
    }

    private void handleIntent(Intent intent)
    {
        if (Intent.ACTION_SEARCH.equals(intent.getAction()))
        {
            String query = intent.getStringExtra(SearchManager.QUERY);
            goSearch(query);
        }
    }


    @Override
    public void onNewIntent(Intent intent)
    {
        setIntent(intent);
        handleIntent(intent);
    }

    private void goSearch(final String s)
    {}
}
4

2 回答 2

0

要在用户键入时连续搜索,请设置查询文本侦听器。

SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() { public boolean onQueryTextChange(String newText) { goSearch(newText); 返回真;}

    public boolean onQueryTextSubmit(String query) 
    {
        goSearch(query);
        return true;
    }
};
searchView.setOnQueryTextListener(queryTextListener);

但是,您的应用程序似乎没有为这种连续搜索设置。您在 MainActivity 的菜单中有 SearchView,但启动 SearchActivity 以显示结果。相反,您的 SearchView 菜单项需要位于将显示结果的同一活动中,并且您的 goSearch() 方法应在找到新结果时更新活动的内容。

于 2013-07-23T19:43:12.920 回答
0

这是我最近编写的一个项目的代码片段,它具有ListView旁边的搜索功能ActionBarSherlock

// Create a new search view on the ActionBar
SearchView searchView = new SearchView(getSherlockActivity().getSupportActionBar().getThemedContext());
// Set the hint inside the search box
searchView.setQueryHint("Search...");
// When the user inputs text, filter the FAQs
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
    public boolean onQueryTextChange(String newText) {
        // adapter.getFilter().filter(newText);
        return true;
    }

    public boolean onQueryTextSubmit(String query) {
        // adapter.getFilter().filter(query);
        return true;
    }
});
// Create a "search" menu option to search the list
MenuItem item = menu.add(Menu.NONE, 0, Menu.NONE, R.string.search).setIcon(R.drawable.ic_action_search).setActionView(searchView);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

只需将注释替换为adapter.getFilter()您需要的任何内容。

于 2013-07-23T23:49:01.057 回答