正如标题所说,我在某些片段中实现搜索小部件时遇到问题。我的问题是,当我使用 inflater 为 onCreateOptionsMenu 中的自定义视图充气时,我发现 SearchView 它始终为空!
如果有人有相同或类似的问题,我将不胜感激!:)
在过去的两天里,我一直在努力解决这个问题……这是布局:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/menu_search"
android:title="Search"
android:icon="@drawable/abs__ic_search"
android:showAsAction="always|collapseActionView"
android:orderInCategory="0"
android:actionViewClass="com.actionbarsherlock.widget.SearchView"/>
<item android:id="@+id/menu_settings"
android:title="Settings"
android:icon="@drawable/jk_uma_button_settings_normal"
android:showAsAction="always"
android:orderInCategory="1"
/>
</menu>
代码
Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_explore, menu);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
Log.i(TAG, "======================FOCUS TRUE============");
}else{
Log.i(TAG, "======================FOCUS FALSE===========");
}
}
});
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "onCreate");
api_key = Configuration.get_prefereence_string(getActivity(), "user_activation_key", null);
user_id = Configuration.get_prefereence_string(getActivity(), "user_id", null);
setHasOptionsMenu(true);
}