当前位置: 首页 > 知识库问答 >
问题:

长时间单击web视图时的水平菜单充气器

狄楷
2023-03-14

我在LongClick上的webview选择有问题。我已经实现了一个在LongClick上启动的定制菜单。但默认菜单也会启动。我正在尝试自定义默认菜单,但我不知道如何捕获用户对一个项目的点击。

我试过下面的,但是菜单正在变得垂直并且隐藏了所选内容,所以我无法选择更多的单词或者更改所选内容。

  @Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
{
    super.onCreateContextMenu(menu, v, menuInfo);


    MenuInflater inflater = getMenuInflater();

    /*
        public void inflate (int menuRes, Menu menu)
            Inflate a menu hierarchy from the specified XML resource. Throws InflateException if there is an error.

        Parameters
            menuRes : Resource ID for an XML layout resource to load (e.g., R.menu.main_activity)
            menu : The Menu to inflate into. The items and submenus will be added to this Menu.

    */
    inflater.inflate(R.menu.menu, menu);
}

@Override
public boolean onContextItemSelected(MenuItem item){
    // Handle the menu item selection
    switch(item.getItemId()){
        case R.id.dict_menu:
            // Render the page again
            Toast.makeText(mContext,"dict_menu",Toast.LENGTH_SHORT).show();
            return true;
        case R.id.q_menu:
            Toast.makeText(mContext,"q_menu",Toast.LENGTH_SHORT).show();
            return true;
        case R.id.hi_menu:
            Toast.makeText(mContext,"hi_menu",Toast.LENGTH_SHORT).show();
            return true;
        default:
            super.onContextItemSelected(item);
    }
    return false;
} 

菜单xml如下所示。

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal">
     <item android:id="@+id/dict_menu"
    android:title="قاموس" />
     <item android:id="@+id/q_menu"
    android:title="اقتباس" />
     <item android:id="@+id/hi_menu"
    android:title="تظليل" />
</menu> 

@Override
public void onActionModeStarted(ActionMode mode) {
    System.out.println("onActionModeStarted");
    if (mActionMode == null)
    {
        mActionMode = mode;
        //mode.setTitle("Dictionary");
        Menu menu = mode.getMenu();
        menu.clear();
        mode.getMenuInflater().inflate(R.menu.menu, menu);//mode.getMenuInflater().inflate(myMenu, menu);
    }
    //System.out.println("onActionModeStarted");
    super.onActionModeStarted(mode);
}

public void onContextualMenuItemClicked(MenuItem item) {
    System.out.println("onContextualMenuItemClicked");
    switch (item.getItemId()) {
        case R.id.dict_menu:
            // do some stuff
            System.out.println("dict_menu");
        Toast.makeText(mContext,"dict_menu",Toast.LENGTH_SHORT).show();
            break;
        case R.id.hi_menu:
            // do some different stuff
            System.out.println("hi_menu");
          Toast.makeText(mContext,"hi_menu",Toast.LENGTH_SHORT).show();
            break;

        case R.id.q_menu:
            // do some different stuff
            System.out.println("q_menu");
           Toast.makeText(mContext,"q_menu",Toast.LENGTH_SHORT).show();
            break;
        default:
            // ...
            super.onContextItemSelected(item);
            break;
    }

    // This will likely always be true, but check it anyway, just in case
    /*if (mActionMode != null) {
        mActionMode.finish();
    }*/
}

@Override
public void onActionModeFinished(ActionMode mode) {
    mActionMode = null;
    super.onActionModeFinished(mode);
    System.out.println("onActionModeFinished");
}

 <?xml version="1.0" encoding="utf-8"?>
 <!---<menu xmlns:android="http://schemas.android.com/apk/res/android"-->
 <menu xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="horizontal">

     <item android:id="@+id/dict_menu"
         android:onClick="onContextualMenuItemClicked"
         android:title="قاموس" />

     <item android:id="@+id/q_menu"
         android:onClick="onContextualMenuItemClicked"
         android:title="اقتباس" />

     <item android:id="@+id/hi_menu"
         android:onClick="onContextualMenuItemClicked"
         android:title="تظليل" />

 </menu>

这将导致以下错误:

  android.view.InflateException: Couldn't resolve menu item onClick        handler onContextualMenuItemClicked in class android.app.ContextImpl
  08-10 09:01:21.602 4931-4931/ W/System.err:     at android.view.MenuInflater$InflatedOnMenuItemClickListener.<init>(MenuInflater.java:243)
  08-10 09:01:21.602 4931-4931/ W/System.err:     at android.view.MenuInflater$MenuState.setItem(MenuInflater.java:464)
  08-10 09:01:21.602 4931-4931/ W/System.err:     at android.view.MenuInflater$MenuState.addItem(MenuInflater.java:498)
  08-10 09:01:21.602 4931-4931/ W/System.err:     at android.view.MenuInflater.parseMenu(MenuInflater.java:191)
  08-10 09:01:21.602 4931-4931/ W/System.err:     at android.view.MenuInflater.inflate(MenuInflater.java:112)
  08-10 09:01:21.602 4931-4931/ W/System.err:     at BookReader.onActionModeStarted(BookReader.java:3346)
  08-10 09:01:21.602 4931-4931/ W/System.err:     at com.android.internal.policy.DecorView.startActionMode(DecorView.java:1034)
  08-10 09:01:21.602 4931-4931/ W/System.err:     at com.android.internal.policy.DecorView.startActionModeForChild(DecorView.java:982)
  08-10 09:01:21.602 4931-4931/ W/System.err:     at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:828)
  08-10 09:01:21.602 4931-4931/ W/System.err:     at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:828)
  08-10 09:01:21.602 4931-4931/ W/System.err:     at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:828)
  08-10 09:01:21.603 4931-4931/ W/System.err:     at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:828)
  08-10 09:01:21.603 4931-4931/ W/System.err:     at android.view.View.startActionMode(View.java:6398)
  08-10 09:01:21.603 4931-4931/ W/System.err:     at org.chromium.content.browser.SelectionPopupController.showActionModeOrClearOnFailure(SelectionPopupController.java:45)
  08-10 09:01:21.603 4931-4931/ W/System.err:     at org.chromium.content.browser.ContentViewCore.onSelectionEvent(ContentViewCore.java:579)
  08-10 09:01:21.603 4931-4931/ W/System.err:     at org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
  08-10 09:01:21.603 4931-4931/ W/System.err:     at org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:7)
  08-10 09:01:21.603 4931-4931/ W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
  08-10 09:01:21.603 4931-4931/ W/System.err:     at android.os.Looper.loop(Looper.java:154)
  08-10 09:01:21.603 4931-4931/ W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6692)
  08-10 09:01:21.603 4931-4931/ W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
  08-10 09:01:21.603 4931-4931/ W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
  08-10 09:01:21.603 4931-4931/ W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
  08-10 09:01:21.603 4931-4931/ W/System.err: Caused by: java.lang.NoSuchMethodException: onContextualMenuItemClicked [interface android.view.MenuItem]
  08-10 09:01:21.603 4931-4931/ W/System.err:     at java.lang.Class.getMethod(Class.java:1981)
  08-10 09:01:21.603 4931-4931/ W/System.err:     at java.lang.Class.getMethod(Class.java:1637)
  08-10 09:01:21.603 4931-4931/ W/System.err:     at android.view.MenuInflater$InflatedOnMenuItemClickListener.<init>(MenuInflater.java:241)
  08-10 09:01:21.603 4931-4931/ W/System.err:   ... 22 more
  08-10 09:01:21.604 4931-4931/ A/chromium: [FATAL:jni_android.cc(243)] Please include Java exception stack in crash report

共有1个答案

南门志
2023-03-14

在托管WebView的活动中,重写OnActionModeStarted(),操作菜单项,并为每个活动分配侦听器。一个例子:

@Override
public void onActionModeStarted(ActionMode mode) {
    super.onActionModeStarted(mode);

    MenuInflater menuInflater = mode.getMenuInflater();
    Menu menu = mode.getMenu();

    menu.clear();
    menuInflater.inflate(R.menu.menu_custom, menu);

    menu.findItem(R.id.custom_one).setOnMenuItemClickListener(new ToastMenuItemListener(this, mode, "One!"));
    menu.findItem(R.id.custom_two).setOnMenuItemClickListener(new ToastMenuItemListener(this, mode, "Two!"));
    menu.findItem(R.id.custom_three).setOnMenuItemClickListener(new ToastMenuItemListener(this, mode, "Three!"));
}

private static class ToastMenuItemListener implements MenuItem.OnMenuItemClickListener {

    private final Context context;
    private final ActionMode actionMode;
    private final String text;

    private ToastMenuItemListener(Context context, ActionMode actionMode, String text) {
        this.context = context;
        this.actionMode = actionMode;
        this.text = text;
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
        actionMode.finish();
        return true;
    }
}
 类似资料:
  • 编辑:我无法在公共void onClick(DialogInterface dialog,int which)函数中获取selectedValue字符串值。 是我的对象

  • 我有一个带有页眉和页脚的主JSP页面,而页面的内容位于另一个JSP页面中。我想点击菜单项并通过加载链接页面 我做什么: 这是index.jsp: 如果我点击第二个链接,我想包括page.jsp,只包含网页的中心部分的代码

  • 这是点击事件。 这是xml 结果将显示在片段中,但抽屉菜单仍然显示。我该怎么做才能关闭它?任何人都给我建议,如何解决这个问题,任何帮助非常感谢。谢谢你。

  • 问题内容: 我试图单击菜单链接,但没有任何运气。它总是显示 异常- 线程“主” org.openqa.selenium.WebDriverException中的异常:未知 错误:元素在点(64,64)处不可单击。其他元素将 获得点击: 我有以下片段 这就是快照的样子 我正在使用以下代码来实现相同目的- 我确实检查了接受点击的DOM中的标签。但是我看到 了 具有一个附加属性,即 ; 使用以下配置: