在操作栏的Andr​​oid搜索项目 [英] Search item on action bar android

查看:250
本文介绍了在操作栏的Andr​​oid搜索项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先看什么我还没有完成。

First of all see what I have done yet.

这是我menu.xml文件的文件。

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>

这是我的 onCreateOptionMenu 功能。我并没有改变它。

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

这是我的 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事件。

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

推荐答案

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

通过这个去。它说明了如何设置搜索视图中的ActionBar和onCreateOptionsMenu添加textchangelistner的搜索查看。

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

这篇关于在操作栏的Andr​​oid搜索项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆