首先看看我做了什么。
这是我的menu.xml文件。
<?xml version="1.0" encoding="utf-8"?>
<item android:id="@+id/friend"
android:title="friend_list"
android:icon="@drawable/friends"
android:showAsAction="ifRoom"/>
<item android:id="@+id/search"
android:title="search"
android:icon="@drawable/search"
android:showAsAction="always|collapseActionView"
android:actionViewClass="android.widget.SearchView" />
<item android:id="@+id/profile"
android:title="@string/profile" />
<item android:id="@+id/settings"
android:title="@string/settings" />
<item android:id="@+id/logout"
android:title="@string/logout" />
</menu>
这是我的onCreateOptionMenu
功能。我没有改变它。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
这是我的onOptionsItemSelected
功能。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.friend:
showFragment(FRIENDLIST, true);
((FriendListActivity)fragments[FRIENDLIST]).populateFriendList(this, resultObj.HomeData.Friends);
return true;
case R.id.logout:
showFragment(SETTINGS, true);
return true;
case android.R.id.home:
showFragment(HOME, false);
return true;
case R.id.search:
if(!fragments[FRIENDLIST].isVisible())
{
showFragment(FRIENDLIST, true);
((FriendListActivity)fragments[3]).populateFriendList(this, resultObj.HomeData.Friends);
}
return true;
default:
return super.onOptionsItemSelected(item);
}
当我点击搜索菜单项时,它会展开并显示要查看的textview。我想要textView的textChanged事件。
答案 0 :(得分:1)
http://developer.android.com/training/search/setup.html
通过这个。它解释了如何在操作栏中设置搜索视图,并在onCreateOptionsMenu中为searchview添加textchangelistner。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
final SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText) {
TextView textView=(TextView)findViewById(R.id.aa);
textView.setText(newText);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
TextView textView=(TextView)findViewById(R.id.aa);
textView.setText(query);
return true;
}
};
searchView.setOnQueryTextListener(queryTextListener);
return true;
}