Search item on action bar android

2019-08-29 00:18发布

First of all see what I have done yet.

This is my menu.xml file.

<?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>

This is my onCreateOptionMenu function. I haven't changed it.

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}

This is my onOptionsItemSelected function.

@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);
}

When I click on search menu item, It expands and show textview to write. I want the textChanged event of that textView.

1条回答
Emotional °昔
2楼-- · 2019-08-29 01:18

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

Go through this. It explains how to setup search view in actionbar and add textchangelistner for searchview in onCreateOptionsMenu.

   @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;
}
查看更多
登录 后发表回答