【问题标题】:Android Webview: Override Contextual Action BarAndroid Webview:覆盖上下文操作栏
【发布时间】:2016-03-07 06:26:29
【问题描述】:

我正在尝试覆盖 Android Webview 中的上下文操作栏。当我长按选定的单词时,会显示自定义操作栏。但是,当我单击操作栏按钮时,什么也没有发生。

似乎没有调用onContextItemSelected() 函数。这是我的代码:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dictionary);
   mWebView = (WebView) findViewById(R.id.wv);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.save_word:
            Toast.makeText(CheckDictionary.this,"Save Word Meaning Successfully",Toast.LENGTH_LONG).show();
            break;
        default:
            break;
    }
    if (mActionMode != null) {
        mActionMode.finish();
    }
    return super.onContextItemSelected(item);
}

@Override
public void onActionModeStarted(ActionMode mode) {
    if (mActionMode == null) {
        mActionMode = mode;
        mode.setTitle("Save Word Meaning");
        Menu menu = mode.getMenu();
        menu.clear();
        mode.getMenuInflater().inflate(R.menu.dictionary_menu, menu);
    }
    super.onActionModeStarted(mode);
}

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

【问题讨论】:

    标签: android webview android-actionbar overriding contextual-action-bar


    【解决方案1】:

    我正在像这样的旧项目上做这项工作:

     private String[] data = {"1", "2", "3", "4", "5", "6", "7", "8", "9","10"};
    
     private SelectionAdapter mAdapter;
    
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
    
         mAdapter = new SelectionAdapter(this,
                     R.layout.row_list_item, R.id.textView1, data);
         setListAdapter(mAdapter);
         getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    
         getListView().setMultiChoiceModeListener(new MultiChoiceModeListener() {
    
             private int nr = 0;
    
             @Override
             public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                 // TODO Auto-generated method stub
                 return false;
             }
    
             @Override
             public void onDestroyActionMode(ActionMode mode) {
                 // TODO Auto-generated method stub
                  mAdapter.clearSelection();
             }
    
             @Override
             public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                 // TODO Auto-generated method stub
    
                 nr = 0;
                 MenuInflater inflater = getMenuInflater();
                 inflater.inflate(R.menu.contextual_menu, menu);
                 return true;
             }
    
             @Override
             public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                 // TODO Auto-generated method stub
                 switch (item.getItemId()) {
    
                     case R.id.item_delete:
                         nr = 0;
                         mAdapter.clearSelection();
                         mode.finish();
                 }
                return false;
             }
    
             @Override
             public void onItemCheckedStateChanged(ActionMode mode, int position,
                     long id, boolean checked) {
                 // TODO Auto-generated method stub
                  if (checked) {
                         nr++;
                         mAdapter.setNewSelection(position, checked);                    
                     } else {
                         nr--;
                         mAdapter.removeSelection(position);                 
                     }
                     mode.setTitle(nr + " selected");
    
             }
         });
    
         getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
    
             @Override
             public boolean onItemLongClick(AdapterView<? arg0, View arg1,
                     int position, long arg3) {
                 // TODO Auto-generated method stub
    
                 getListView().setItemChecked(position, !mAdapter.isPositionChecked(position));
                 return false;
             }
         });
     }
    
     private class SelectionAdapter extends ArrayAdapter<String {
    
         private HashMap<Integer, Boolean mSelection = new HashMap<Integer, Boolean();
    
         public SelectionAdapter(Context context, int resource,
                 int textViewResourceId, String[] objects) {
             super(context, resource, textViewResourceId, objects);
         }
    
         public void setNewSelection(int position, boolean value) {
             mSelection.put(position, value);
             notifyDataSetChanged();
         }
    
         public boolean isPositionChecked(int position) {
             Boolean result = mSelection.get(position);
             return result == null ? false : result;
         }
    
         public Set<Integer getCurrentCheckedPosition() {
             return mSelection.keySet();
         }
    
         public void removeSelection(int position) {
             mSelection.remove(position);
             notifyDataSetChanged();
         }
    
         public void clearSelection() {
             mSelection = new HashMap<Integer, Boolean();
             notifyDataSetChanged();
         }
    
         @Override
         public View getView(int position, View convertView, ViewGroup parent) {
             View v = super.getView(position, convertView, parent);//let the adapter handle setting up the row views
             v.setBackgroundColor(getResources().getColor(android.R.color.background_light));
    
    
             if (mSelection.get(position) != null) {
                 v.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_light);
    
             }
             return v;
         }
     } 
    

    您可以根据需要对这段代码进行更改...

    【讨论】:

      【解决方案2】:

      试试下面的步骤

      public class MyWebView extends WebView {
          MyActivity theListener;
          Context context;
          GestureDetector gd;
      
          public MyWebView(Context context, AttributeSet attributes) {
              super(context, attributes);
              this.context = context;
              gd = new GestureDetector(context, sogl);
          }
      
          // This is new
          public void setListener(MyActivity l) {
              theListener = l;
          }
      
          @Override
          public boolean onTouchEvent(MotionEvent event) {
              return gd.onTouchEvent(event);
          }
      
          GestureDetector.SimpleOnGestureListener sogl =
                      new GestureDetector.SimpleOnGestureListener() {
      
              public boolean onDown(MotionEvent event) {
                  return true;
              }
      
              public void onLongPress(MotionEvent event) {
                  theListener.onLongClick(MyWebView.this);
              }
          };
      }
      

      在活动中

      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.layout);
      
          MyWebView mwv = (MyWebView) findViewById(R.id.mwv);
          registerForContextMenu(mwv);
      }
      
      public boolean onLongClick(View v) {
          openContextMenu(v);
          return true;
      }
      
      @Override
      public void onCreateContextMenu(ContextMenu menu, View v,
                          ContextMenu.ContextMenuInfo menuInfo) {
          super.onCreateContextMenu(menu, v, menuInfo);
          MenuInflater inflater = getMenuInflater();
          inflater.inflate(R.menu.context, menu);
      }
      

      【讨论】:

      • 感谢您的回复,但我不太了解 MyWebView 类的目的。我在哪里执行 onContextItemSelected 的操作?喜欢当我点击按钮 R.id.save_word 时执行操作?
      • 我们需要覆盖 web 视图的长按,这就是为什么我在你的活动中自定义 web 视图只是覆盖 @Override public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.save_word: Toast.makeText(CheckDictionary.this,"保存单词含义成功",Toast.LENGTH_LONG).show();休息;默认值:中断; } if (mActionMode != null) { mActionMode.finish(); } 返回 super.onContextItemSelected(item); }
      • 但对我不起作用。错误出现在 registerForContextMenu(mwv);
      • 通过此链接developer.android.com/intl/es/reference/android/app/… 让我交叉检查它的原因,谢谢
      • 但是我还是看不懂 MyWebView mwv = (MyWebView) findViewById(R.id.mwv);对于 R.id.mwv,它是布局中 webview 的 id。它可能有语法错误。它是 webview id 的对象。如果它由 MyWebView 对象声明,它可能有语法错误。感谢您的更多解释。
      猜你喜欢
      • 2013-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多