Android WebView - Text Selection Listener in 2018

本秂侑毒 提交于 2019-12-24 07:35:41

问题


I will create an app that user will be enter the website url to my app. Then I am showing this page in my app using WebView.

As you know, when user clicks the any text in the context a little bit long, android cursor will appear then we can select text as many as we want.

After selection, we will see that "COPY, SHARE, SELECT ALL" etc..

My question is that when user selects text, I want to show them different options. Let's say "MyCOPY, SendTwitter, SendMessage".

How can i do that?

What I did so far?

I am just creating bar at the top of the app. But I don't want this.

Here is the code:

private WebView view;
private final String TAG = MainActivity.class.getSimpleName();
private ActionMode actionMode;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this.view = findViewById(R.id.webView);
    view.loadUrl("https://stackoverflow.com/questions/28385768/android-how-to-check-for-successful-load-of-url-when-using-webview-loadurl");
    view.setWebViewClient(new MyWebViewClient());
    Log.d(TAG, view.getUrl());
    view.setOnLongClickListener((v) -> {
        if (actionMode != null)
            return false;
        actionMode = startSupportActionMode(actionCallBack);
        return true;
    });
}

Where startSupportActionMode(actionCallBack) is

private ActionMode.Callback actionCallBack = new ActionMode.Callback() {

    @Override
    public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
        actionMode.getMenuInflater().inflate(R.menu.custommenu, menu);
        actionMode.setTitle("Choose");
        return true;
    }

    @Override
    public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
        return false;
    }

    @Override
    public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
        switch (menuItem.getItemId()){
            case R.id.example_item_1:
                Toast.makeText(MainActivity.this, "Option 1 selected", Toast.LENGTH_SHORT).show();
                actionMode.finish();
                return true;
            case R.id.example_item_2 :
                Toast.makeText(MainActivity.this, "Option 2 selected", Toast.LENGTH_SHORT).show();
                actionMode.finish();
                return true;
            default:
                return false;
        }
    }

    @Override
    public void onDestroyActionMode(ActionMode actionMode) {
        actionMode = null;
    }
};

回答1:


You can implement the ActionMode.Callback interface to create your own menu upon selection.

An action mode's lifecycle is as follows:

onCreateActionMode(ActionMode, Menu) once on initial creation

onPrepareActionMode(ActionMode, Menu) after creation and any time the ActionMode is invalidated onActionItemClicked(ActionMode, MenuItem) any time a contextual action button is clicked onDestroyActionMode(ActionMode) when the action mode is closed

just make sure that your text views allow for text selection (android:textIsSelectable="true")

private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {

    // Called when the action mode is created; startActionMode() was called
    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        // Inflate a menu resource providing context menu items
        MenuInflater inflater = mode.getMenuInflater();
        inflater.inflate(R.menu.context_menu, menu);
        return true;
    }

    // Called each time the action mode is shown. Always called after onCreateActionMode, but
    // may be called multiple times if the mode is invalidated.
    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        return false; // Return false if nothing is done
    }

    // Called when the user selects a contextual menu item
    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_share:
                shareCurrentItem();
                mode.finish(); // Action picked, so close the CAB
                return true;
            default:
                return false;
        }
    }

    // Called when the user exits the action mode
    @Override
    public void onDestroyActionMode(ActionMode mode) {
        mActionMode = null;
    }
};

then call startActionMode() to enable the contextual action mode when appropriate (source), such as inside a setOnLongClickListener



来源:https://stackoverflow.com/questions/53325530/android-webview-text-selection-listener-in-2018

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