Android AppCompatActivity的ActionBar之SearchView、ShareActionProvider以及menu
Android早期推出的ActionBarActivity被Android谷歌官方废弃,推荐使用AppCompatActivity替代ActionBarActivity。在实际的开发中现在也基本上不再怎么使用ActionBar,而是使用最新的Toolbar替代ActionBar了,但是ActionBar也有其自身比较有特点的地方,比如ActionBar自身对SearchView,ShareActionProvider的良好支持,使得ActionBar在某些应用开发场景中依然有其用武之地。
现在给出一个例子说明如何在一个ActionBar中实现SearchView和ShareActionProvider。
先把完整的代码实现给出,然后结合代码加以说明。
测试用的主Activity,MainActivity.java:
package zhangphil.toolbar; import android.content.Intent; import android.os.Bundle; import android.support.v4.view.MenuItemCompat; import android.support.v4.view.MenuItemCompat.OnActionExpandListener; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.SearchView; import android.support.v7.widget.SearchView.OnQueryTextListener; import android.support.v7.widget.ShareActionProvider; import android.view.Menu; import android.view.MenuItem; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActionBar ab = this.getSupportActionBar(); ab.setDisplayHomeAsUpEnabled(true); } private Intent getMyIntent() { Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("text/plain"); intent.putExtra(Intent.EXTRA_SUBJECT, "主题"); intent.putExtra(Intent.EXTRA_TEXT, "我的文字内容,zhangphil text"); return intent; } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); MenuItem searchItem = menu.findItem(R.id.search); SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem); searchView.setOnQueryTextListener(new OnQueryTextListener() { @Override public boolean onQueryTextChange(String queryText) { Toast.makeText(getApplicationContext(), "onQueryTextChange:" + queryText, Toast.LENGTH_SHORT).show(); return true; } @Override public boolean onQueryTextSubmit(String queryText) { Toast.makeText(getApplicationContext(), "onQueryTextSubmit:" + queryText, Toast.LENGTH_SHORT).show(); return true; } }); OnActionExpandListener expandListener = new OnActionExpandListener() { @Override public boolean onMenuItemActionCollapse(MenuItem item) { Toast.makeText(getApplicationContext(), "onMenuItemActionCollapse", Toast.LENGTH_SHORT).show(); return true; } @Override public boolean onMenuItemActionExpand(MenuItem item) { Toast.makeText(getApplicationContext(), "onMenuItemActionExpand", Toast.LENGTH_SHORT).show(); return true; } }; MenuItemCompat.setOnActionExpandListener(searchItem, expandListener); MenuItem shareItem = menu.findItem(R.id.share); ShareActionProvider shareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem); shareActionProvider.setShareIntent(getMyIntent()); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.share: Toast.makeText(getApplicationContext(), "分享", Toast.LENGTH_SHORT).show(); return true; case R.id.setting: Toast.makeText(getApplicationContext(), "设置", Toast.LENGTH_SHORT).show(); return true; case R.id.about: Toast.makeText(getApplicationContext(), "关于", Toast.LENGTH_SHORT).show(); return true; default: return super.onOptionsItemSelected(item); } } }
在本例中,MainActivity.java不依赖任何布局文件,只是需要加载menu菜单,所以需要在res/menu/目录下写一个menu菜单的XML文件,定义menu的item,此代码文件在本例中称之为main.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" > <item android:id="@+id/search" android:title="搜索" app:actionViewClass="android.support.v7.widget.SearchView" app:showAsAction="ifRoom|collapseActionView"/> <item android:id="@+id/share" android:title="分享" app:actionProviderClass="android.support.v7.widget.ShareActionProvider" app:showAsAction="ifRoom|withText"/> <item android:id="@+id/setting" android:title="设置" app:showAsAction="never"/> <item android:id="@+id/about" android:title="关于" app:showAsAction="ifRoom"/> </menu>
在MainActivity.java中的一个方法体中,重写:
public boolean onCreateOptionsMenu(Menu menu);
方法将依据res/menu/main.xml文件创建菜单。在这个方法中,为id为search的menu item创建响应搜索事件。同时,附带的为搜索item绑定了展开和收缩事件:
OnActionExpandListener expandListener = new OnActionExpandListener() { @Override public boolean onMenuItemActionCollapse(MenuItem item) { Toast.makeText(getApplicationContext(), "onMenuItemActionCollapse", Toast.LENGTH_SHORT).show(); return true; } @Override public boolean onMenuItemActionExpand(MenuItem item) { Toast.makeText(getApplicationContext(), "onMenuItemActionExpand", Toast.LENGTH_SHORT).show(); return true; } }; MenuItemCompat.setOnActionExpandListener(searchItem, expandListener);
对于菜单中分享菜单share item,则为其设置了ShareActionProvider,提供分享事件的数据和动作支持:
MenuItem shareItem = menu.findItem(R.id.share); ShareActionProvider shareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem); shareActionProvider.setShareIntent(getMyIntent());
其他次要普通的菜单选择事件则放在
public boolean onOptionsItemSelected(MenuItem item);
中完成。
图1是代码运行后,点击分享icon切换到微信的ActionBar状态:
图2则是点击了搜索item时候的ActionBar状态: