sample_menu.xml
android.widget.SearchViewを使う
1 2 3 4 5 6 7 8 9 10 |
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/searchWord" android:icon="@android:drawable/ic_menu_search" android:showAsAction="always" android:actionViewClass="android.widget.SearchView"/> </menu> |
Activity
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
//OnQueryTextListener をimplements public class Main extends Activity implements OnQueryTextListener { protected void onCreate(Bundle savedInstanceState) { ActionBar actionBar = getActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); } @Override public boolean onCreateOptionsMenu(Menu menu){ MenuInflater inflater = getMenuInflater(); inflater .inflate(R.menu.sample_menu, menu); MenuItem searchItem = menu.findItem(R.id.searchWord); final SearchView searchView = (SearchView) searchItem.getActionView(); searchView.setOnQueryTextListener(this); return true; } @Override public boolean onQueryTextSubmit(String query) { //検索実行時のキーワードがqueryに入る return false; } @Override public boolean onQueryTextChange(String newText) { // TODO 自動生成されたメソッド・スタブ return false; } } |