[Android]聊聊ActionMode
最近一段时间都没有更新文章,趁工作之余,更新一篇。
今天介绍一个很常见效果也最容易被忽略的弹出框:ActionMode。主要是ActionMode使用和自己使用过程中遇到的一些问题,相对还是比较简单的。
1、ActionMode的基本使用
2、使用ActionMode遇到的一些问题
1、ActionMode的基本使用
主要分两步:
1、实现ActionMode.Callback接口
2、调用startActionMode或者startSupportActionMode
1、实现ActionMode.Callback接口
对于ActionMode.Callback,我们需要实现其四个方法:
new ActionMode.Callback() {
//Called when action mode is first created.
// The menu supplied will be used to generate action buttons for the action mode
//当ActionMode第一次被创建的时候调用
//可在这里初始化menu
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.menu, menu);
menu.findItem(R.id.action_edit).setIcon(R.drawable.ic_launcher);
return true;//返回true表示action mode会被创建 false if entering this mode should be aborted.
}
//
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
//当ActionMode被点击时调用
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.action_edit:
rename();
return true;
}
return false;
}
//Called when an action mode is about to be exited and destroyed.
//当ActionMode退出销毁时调用
@Override
public void onDestroyActionMode(ActionMode mode) {
}
};
2、调用startActionMode或者startSupportActionMode开启弹出框
ActionMode mActionMode = = startSupportActionMode(actionModeCallback)
可以为ActionMode设置属性:mActionMode.setTitle、mActionMode.setSubtitle
2、使用ActionMode遇到的坑
坑一:ActionMode把Toolbar压下去了
在theme加入配置:
<item name="windowActionModeOverlay">true</item>
<item name="android:windowActionModeOverlay">true</item>
坑二:修改ActionMode背景
在theme加入配置:
<item name="android:actionModeBackground">@color/colorPrimary</item>
<item name="actionModeBackground">@color/colorPrimary</item>
坑三:屏蔽掉WebView的ActionMode用自己的选择复制
通过js与webView互调来实现文本复制
stackoverflow链接:http://stackoverflow.com/questions/6058843/android-how-to-select-texts-from-webview
public class MyCustomWebView extends WebView{
private Context context;
// override all other constructor to avoid crash
public MyCustomWebView(Context context) {
super(context);
this.context = context;
WebSettings webviewSettings = getSettings();
webviewSettings.setJavaScriptEnabled(true);
// add JavaScript interface for copy
addJavascriptInterface(new WebAppInterface(context), "JSInterface");
}
// setting custom action bar
private ActionMode mActionMode;
private ActionMode.Callback mSelectActionModeCallback;
private GestureDetector mDetector;
// this will over ride the default action bar on long press
@Override
public ActionMode startActionMode(ActionMode.Callback callback) {
ViewParent parent = getParent();
if (parent == null) {
return null;
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
String name = callback.getClass().toString();
if (name.contains("SelectActionModeCallback")) {
mSelectActionModeCallback = callback;
mDetector = new GestureDetector(context,
new CustomGestureListener());
}
}
CustomActionModeCallback mActionModeCallback = new CustomActionModeCallback();
return parent.startActionModeForChild(this, mActionModeCallback);
}
private class CustomActionModeCallback implements ActionMode.Callback {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mActionMode = mode;
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.copy:
getSelectedData();
mode.finish();
return true;
case R.id.share:
mode.finish();
return true;
default:
mode.finish();
return false;
}
}
@Override
public void onDestroyActionMode(ActionMode mode) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
clearFocus();
}else{
if (mSelectActionModeCallback != null) {
mSelectActionModeCallback.onDestroyActionMode(mode);
}
mActionMode = null;
}
}
}
private void getSelectedData(){
String js= "(function getSelectedText() {"+
"var txt;"+
"if (window.getSelection) {"+
"txt = window.getSelection().toString();"+
"} else if (window.document.getSelection) {"+
"txt = window.document.getSelection().toString();"+
"} else if (window.document.selection) {"+
"txt = window.document.selection.createRange().text;"+
"}"+
"JSInterface.getText(txt);"+
"})()";
// calling the js function
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
evaluateJavascript("javascript:"+js, null);
}else{
loadUrl("javascript:"+js);
}
}
private class CustomGestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onSingleTapUp(MotionEvent e) {
if (mActionMode != null) {
mActionMode.finish();
return true;
}
return false;
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// Send the event to our gesture detector
// If it is implemented, there will be a return value
if(mDetector !=null)
mDetector.onTouchEvent(event);
// If the detected gesture is unimplemented, send it to the superclass
return super.onTouchEvent(event);
}
public class WebAppInterface {
Context mContext;
WebAppInterface(Context c) {
mContext = c;
}
@JavascriptInterface
public void getText(String text) {
// put selected text into clipdata
ClipboardManager clipboard = (ClipboardManager)
mContext.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("simple text",text);
clipboard.setPrimaryClip(clip);
// gives the toast for selected text
Toast.makeText(mContext, text, Toast.LENGTH_SHORT).show();
}
}
}
menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/copy"
android:icon="@android:drawable/ic_dialog_email"
app:showAsAction="always"
android:title="copy">
</item>
<item
android:id="@+id/share"
android:icon="@android:drawable/ic_dialog_alert"
app:showAsAction="always"
android:title="share">
</item>
</menu>
坑四:ActionMode显示时状态栏闪动,表现为变黑之后恢复正常
在onDestroyActionMode方法加入如下代码:
@Override
public final void onDestroyActionMode(ActionMode mode) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
new Handler().postDelayed(new Runnable() {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void run() {
try {
mActivity.getWindow().setStatusBarColor(mActivity.getResources().getColor(android.R.color.transparent));
}
catch(Exception e)
{
e.printStackTrace();
}
}
}, 400);
}
//TODO ...
}
[Android]聊聊ActionMode的更多相关文章
- Android新的menu实现——ActionMode
Android的menu有多种实现方式,以前写过一篇Android中五种常用的menu(菜单),这里介绍一种新的menu实现方式:ActionMode.ActionMode是Android 3.0以后 ...
- (转)Android新的menu实现——ActionMode
Android的menu有多种实现方式,以前写过一篇Android中五种常用的menu(菜单),这里介绍一种新的menu实现方式:ActionMode.ActionMode是Android 3.0以后 ...
- Android — 长按ListView 利用上下文菜单(ActionMode) 进行批量事件处理
好久没写博客拉``````` 近期最终略微闲一点了``````` 无聊拿手机清理短信.发现批量事件的处理还是挺管用的`````` 那么自己也来山寨一记看看效果吧````` 闲话少说,首先,我们来看下手 ...
- Android Contextual Menus之二:contextual action mode
Android Contextual Menus之二:contextual action mode 接上文:Android Contextual Menus之一:floating context me ...
- [zt] Android中使用List列表
原文地址:http://www.vogella.com/tutorials/AndroidListView/article.html 1. Android and Lists 1.1. Using l ...
- Android ActionBar 一步一步分析 (转)
原文摘自:http://blog.csdn.net/android2me/article/details/8874846 1.Action Bar 介绍 我们能在应用中看见的actionbar一般就是 ...
- ActionMode 就记这么一点,不能更多了
话说程序猿都是段子手,看到有的程序猿写文章,前面都会先写一个段子,我这么有幽默感的段子手,也决定效仿一下. "段子." 写完段子,下面开始进入正题. 今天要说的 ActionMod ...
- 【凯子哥带你夯实应用层】使用ActionMode实现有删除动画的多选删除功能
转载请注明出处:http://blog.csdn.net/zhaokaiqiang1992 ActionMode是3.0之后.官方推荐的一种上下文菜单的实现方式,在之前一直用的是Co ...
- Android EditText禁止复制粘贴
1,自定义EditText package com.example.ui; import android.annotation.SuppressLint; import android.content ...
随机推荐
- [C#]设计模式-建造者模式-创建型模式
介绍完工厂模式,现在来看一下建造者模式.建造者模式就是将一系列对象组装为一个完整对象并且返回给用户,例如汽车,就是需要由各个部件来由工人建造成一个复杂的组合实体,这个复杂实体的构造过程就被外部化到一个 ...
- [LeetCode] Bulb Switcher II 灯泡开关之二
There is a room with n lights which are turned on initially and 4 buttons on the wall. After perform ...
- [LeetCode] Find K Closest Elements 寻找K个最近元素
Given a sorted array, two integers k and x, find the k closest elements to x in the array. The resul ...
- 基于webpack的React项目搭建(三)
前言 搭建好前文的开发环境,已经可以进行开发.然而实际的项目中,不同环境有着不同的构建需求.这里就将开发环境和生产环境的配置单独提取出来,并做一些简单的优化. 分离不同环境公有配置 不同环境虽然有不同 ...
- POJ 1721 CARDS
Alice and Bob have a set of N cards labelled with numbers 1 ... N (so that no two cards have the sam ...
- 51 nod 1023 石子归并 V3(GarsiaWachs算法)
1023 石子归并 V3基准时间限制:2 秒 空间限制:131072 KB 分值: 320 难度:7级算法题 N堆石子摆成一条线.现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆石子合并成新的一 ...
- bzoj2683简单题 cdq分治
2683: 简单题 Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 1803 Solved: 731[Submit][Status][Discuss] ...
- Gradle学习之基础篇
一.gradle基础概念 Gradle是一个基于Apache Ant和Apache Maven概念的项目自动化构建工具.Gradle抛弃了基于各种繁琐的XML,使用一种基于Groovy的特定领域语言( ...
- django rest-framework 3.类 实现restful
上节提到过,REST框架分别提供了对函数和类的装饰器,之前已经都是通过函数来写视图函数的,现在来尝试使用class 类来实现视图函数 使用基于类编写API视图,允许重用常用的功能,减少代码重复. 一. ...
- Django中数据查询(万能下换线,聚合,F,Q)
数据查询中万能的下划线基本用法: __contains: 包含 __icontains: 包含(忽略大小写) __startswith: 以什么开头 __istartswith: 以什么开头(忽略大小 ...