Webview Add Menu Item to TextSelection Menu

淺唱寂寞╮ 提交于 2019-12-25 12:52:22

问题


I am having an issue with android webview default text selection. what i want to do is add an item to the default menu which appears on text selection in webview

The functionality i want to acieve is add a button to left side of select all. how can this be done


回答1:


you need to override the WebView class for this you need to use this code i am giving you demo code for changing defaut CAB after selection you can use your own contextual menu

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 is

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">



来源:https://stackoverflow.com/questions/23737510/webview-add-menu-item-to-textselection-menu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!