res/menu/activity_main.xml
1
2
3
4
5
6
7
8
9
10
11
|
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/menu_search"
android:actionViewClass="android.widget.SearchView"
android:icon="@drawable/ic_action_search"
android:orderInCategory="1"
android:showAsAction="always"
android:title="@string/menu_search"/>
</menu>
|
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
30
31
32
33
|
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.activity_main, menu); // 옵션메뉴 지정.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setQueryHint("두 자 이상 입력하세요");
searchView.setOnQueryTextListener(queryTextListener);
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
if(null!=searchManager ) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
}
// 검색필드를 항상 표시하고싶을 경우false, 아이콘으로 보이고 싶을 경우 true
searchView.setIconifiedByDefault(true);
}
return;
}
private OnQueryTextListener queryTextListener = new OnQueryTextListener() {
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override
public boolean onQueryTextSubmit(String query) {
InputMethodManager imm= (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
searchView.setQuery("", false);
searchView.setIconified(true);
Toast.makeText(getActivity(), "search 결과", Toast.LENGTH_LONG).show();
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
return false;
}
};
|
댓글 없음:
댓글 쓰기