如何在Android中覆盖Web视图文本选择菜单 [英] How to override web view text selection menu in android
问题描述
基本的android的网络文本选择菜单如下图所示.它具有复制,共享,全选,网络搜索等选项.
The basic android's web text selection menu is as shown in image attached below. It has options like copy, share, select all, web search.
我想跳过这些菜单,并希望将它们作为自己的菜单列表,例如标记颜色",标记为小鬼"等.我研究了有关堆栈溢出时上下文菜单的大多数可用问题.问题的大部分与上下文菜单有关,但没有给出预期的结果.我想要像下面图片一样的菜单
I want to over ride this menus and want them as my own menu list like "mark colour", "mark as imp" etc. I look around most of the questions available about context menu on stack overflow. The most of the question relate with context menu but not giving result as expected. I want menu like below image
当我执行选择时,android监视器会显示一些视图创建表单,例如viewRoot
When I perform selection android monitor shows some view creation form viewRoot like
D/ViewRootImpl: #1 mView = android.widget.PopupWindow$PopupDecorView{648898f V.E...... ......I. 0,0-0,0}
D/ViewRootImpl: #1 mView = android.widget.PopupWindow$PopupDecorView{a66541c V.E...... ......I. 0,0-0,0}
D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1
D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1
如何实现这种实现?
我也经历了 https://github.com/naoak/WebViewMarker ,但没有得到正确的结果
I also gone through https://github.com/naoak/WebViewMarker but not getting proper result.
我做了什么?
我扩展了android的WebView,并且希望支持最低的SDK19.当我长按时,我得到了长按事件,但无法获得此类菜单创建api调用.
I extend WebView of android and I want to make support for minimum SDK 19. When I perform long press I got long press event but I can't get such menus creation api calls.
推荐答案
此解决方案不依赖于Activity的Action模式,并且适用于所有android平台
我尝试给出答案,但超出了字符数限制,因此我要放置一些代码部分
参考链接1用于在Web视图上选择
用于制作Web视图标记的参考链接2
以上两个链接确实起着重要作用,并且是由一些出色的开发人员开发的.首先需要从参考链接上对TextSelectionSupport类进行一些研究.我在TextSelectionSupport类中自定义了两行代码,以在此处的Selection Listener中获得选择矩形.
从此处克隆示例项目 https://github.com/ab-cse-2014/WebViewSelection.git
Clone the Sample Project from here https://github.com/ab-cse-2014/WebViewSelection.git
请参见CustomWebView的实现和TextSelectionSupport类的使用.
See The implementation of CustomWebView and Use Of TextSelectionSupport class.
这是我在项目中的Web视图类
This is my web view class in project
import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebView;
import android.widget.PopupWindow;
import android.widget.Toast;
import com.cse.webviewtextselection.R;
import com.cse.webviewtextselection.webviewmaker.TextSelectionSupport;
public class CustomWebView extends WebView {
private final String TAG = this.getClass().getSimpleName();
private Context mContext;
private TextSelectionSupport mTextSelectionSupport;
private PopupWindow mPopupWindow;
private int currentTop;
public CustomWebView(Context context) {
super(context);
mContext = context;
initSetUp();
preparePopupWindow();
}
public CustomWebView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
initSetUp();
preparePopupWindow();
}
public CustomWebView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
initSetUp();
preparePopupWindow();
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public CustomWebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
mContext = context;
initSetUp();
preparePopupWindow();
}
private void initSetUp() {
mTextSelectionSupport = TextSelectionSupport.support((AppCompatActivity) mContext, this);
mTextSelectionSupport.setSelectionListener(new TextSelectionSupport.SelectionListener() {
@Override
public void startSelection() {
}
@Override
public void selectionChanged(String text, Rect rect) {
Toast.makeText(mContext, text, Toast.LENGTH_SHORT).show();
showPopAtLocation(mPopupWindow, rect.left, rect.top);
}
@Override
public void endSelection() {
if (mPopupWindow != null) {
mPopupWindow.dismiss();
}
}
});
}
private void preparePopupWindow() {
LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View customPopupView = layoutInflater.inflate(R.layout.custom_popup_layout, null);
mPopupWindow = new PopupWindow(customPopupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
}
private void showPopAtLocation(PopupWindow mPopupWindow, int x, int y) {
if (mPopupWindow != null) {
if (currentTop != 0 || currentTop > ((AppCompatActivity)mContext).getWindow().getDecorView().getHeight()) {
if (y > currentTop) {
y -= currentTop;
}
}
Log.d("Current Top : ", String.valueOf(currentTop));
Log.d("Y : ", String.valueOf(y));
//mPopupWindow.showAtLocation(((AppCompatActivity)mContext).findViewById(R.id.parentRelativeLayout), Gravity.NO_GRAVITY, x, y);
mPopupWindow.showAtLocation(((AppCompatActivity)mContext).getWindow().getDecorView(), Gravity.NO_GRAVITY, x, y);
}
}
@Override
protected void onScrollChanged(int newLeft, int newTop, int oldLeft, int oldTop) {
currentTop = newTop;
super.onScrollChanged(newLeft, newTop, oldLeft, oldTop);
}
}
自定义弹出菜单XML,例如android的智能文本选择(custom_popup_layout.xml)
Custom Popup Menu XML like androids smart text selection(custom_popup_layout.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myCustomMenuLinearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@android:color/transparent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@android:color/white"
android:elevation="5dp"
android:layout_margin="12dp">
<TextView
android:id="@+id/menuOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mark With Color"
android:textColor="@android:color/black"
android:padding="10dp"
android:maxLines="1"/>
<TextView
android:id="@+id/menuTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mark As Important"
android:textColor="@android:color/black"
android:padding="10dp"
android:maxLines="1"/>
<TextView
android:id="@+id/menuThree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show More"
android:textColor="@android:color/black"
android:padding="10dp"
android:maxLines="1"/>
</LinearLayout>
</LinearLayout>
输出屏幕截图
这篇关于如何在Android中覆盖Web视图文本选择菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!