4

我已经成功地让我的操作栏出现在 android 中。我现在正在尝试实现搜索。我试图从小处着手,至少在尝试对它做任何事情之前看看我是否可以得到搜索词。

我在这里遵循了本教程:

http://developer.android.com/training/search/setup.html

并创建了一个看起来像这样的 SearchResultsActivity 类来处理搜索。

import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;


public class SearchResultsActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        //possible more code
        handleIntent(getIntent());
    }

    @Override
    protected void onNewIntent(Intent intent) {
        //possible more code
        handleIntent(intent);
    }

    private void handleIntent(Intent intent) {

        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            //use the query to search your data somehow

            //toast error test
            Context context = getApplicationContext();
            CharSequence text = "Hello toast!";
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, query, duration);
            toast.show();

        }
    }

    ///more code

}

我尝试添加一个 toast 以查看当我输入文本时是否会单击搜索是否会弹出 toast。现在没有吐司出现。

4

1 回答 1

15

我正在做一个类似的项目,在操作栏中有一个搜索小部件。这仅与 API 11+ 兼容,因此没有姜饼,但看起来更好。您仍然需要设置一个 searchable.xml 并在清单中添加适当的元数据。我不确定您将如何实现这一点,但下面的代码会加载一个 ListView,然后添加字符串。onCreateOptionsMenu() 部分配置搜索小部件并根据估算的文本过滤 ListView。希望能帮助到你!

    public class ListOfMathCourses extends ListActivity  {

    ArrayAdapter<String> adapter;
    ListView list;
    String[] math = new String[] {"Pre-Algebra", "Algebra I", "Algebra II", "Geometry", "Calculus"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.startingpoint);
    list = (ListView) findViewById(android.R.id.list);
    adapter = new ArrayAdapter<String>(this, R.layout.listviewrow, math);
    list.setAdapter(adapter);
    getListView().setTextFilterEnabled(true);


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {        

        getMenuInflater().inflate(R.menu.menu_search, menu);
        //getMenuInflater().inflate(R.menu.action, menu);   

        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();

            searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
            searchView.setIconifiedByDefault(false);   

        SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() 
        {
            @Override
            public boolean onQueryTextChange(String newText) 
            {
                // this is your adapter that will be filtered
                adapter.getFilter().filter(newText);
                return true;
            }
            @Override
            public boolean onQueryTextSubmit(String query) 
            {
                // this is your adapter that will be filtered
                adapter.getFilter().filter(query);
                return true;
            }
        };
        searchView.setOnQueryTextListener(queryTextListener);

        return super.onCreateOptionsMenu(menu);

    }

}

于 2013-05-31T00:15:21.517 回答