Webview将菜单项添加到TextSelection菜单

时间:2014-05-19 12:15:18

标签: android webview android-webview

我遇到了android webview默认文本选择的问题。我想要做的是将一个项目添加到默认菜单中,该菜单出现在webview中的文本选择

I want to Add a Button to left of select all

我想要的功能是在select all的左侧添加一个按钮。怎么做呢

1 个答案:

答案 0 :(得分:2)

你需要覆盖WebView类,你需要使用这个代码我给你的演示代码,用于在选择后更改defaut CAB,你可以使用自己的上下文菜单

public class MyWebView  extends WebView{
CustomizedSelectActionModeCallback actionModeCallback;
public Context context;
public MyWebView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
    this.context=context;
}
@Override
public ActionMode startActionMode(ActionMode.Callback callback) {
    // TODO Auto-generated method stub
    //      ViewParent parent = getParent();
   //        if (parent == null) {
     //            return null;
     //        }
    actionModeCallback = new CustomizedSelectActionModeCallback();
    return startActionModeForChild(this,actionModeCallback);

}

public class CustomizedSelectActionModeCallback implements ActionMode.Callback{

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        // TODO Auto-generated method stub
        mode.getMenuInflater().inflate(R.menu.contextual_menu, menu);
        return true;

    }

    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        // TODO Auto-generated method stub
        mode.setTitle("CheckBox is Checked");

        return false;
    }

    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        // TODO Auto-generated method stub
        switch (item.getItemId()) {
        case R.id.item_delete:
            clearFocus();
            Toast.makeText(getContext(), "This is my test click", Toast.LENGTH_LONG).show();
            break;

        default:
            break;
        }
        return false;
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {
        // TODO Auto-generated method stub


        clearFocus(); // this  is not clearing the text in my device having version 4.1.2
        actionModeCallback=null;

    }

}

}

MainActivity.java

public class MainActivity extends Activity {

MyWebView web_view;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    web_view= (MyWebView)findViewById(R.id.webView1);
    web_view.loadUrl("file:///android_asset/menu_pages/Chemchapter1/OEBPS/Text/07_Chapter01.html");// you can load your html here


}

}

<强> activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${packageName}.${activityClass}" >


<com.rstm.webviewcheck.MyWebView
    android:id="@+id/webView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</RelativeLayout>  

<强> contextual_menu.xml

<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item

android:id="@+id/item_delete"

android:icon="@android:drawable/ic_menu_delete"

android:showAsAction="ifRoom|withText"

android:title="Delete"

android:titleCondensed="Delete">

image description