Android webview在longClick和Selection模式下触发自定义CAB

时间:2014-07-04 09:51:13

标签: android webview

我使用此代码覆盖webview上下文操作栏但在长按期间出现CAB但没有文本选择我怎样才能在文本选择模式下触发CAB?

   public class MainActivity extends Activity {
   ActionMode.Callback mCallback;
   ActionMode mMode;
   WebView webview;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    webview=webview = (WebView) findViewById(R.id.webView1);
    webview.loadUrl("http://www.google.com");


    webview.setOnLongClickListener(new OnLongClickListener() {
        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        @SuppressLint("NewApi")
        @Override
        public boolean onLongClick(View v) {
            // TODO Auto-generated method stub
            if (mMode != null)
                return false;
            else
                mMode = startActionMode(mCallback);
            return true;

        }
    });



    mCallback = new ActionMode.Callback() {
        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }

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

        @SuppressLint("NewApi")
        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            mode.setTitle("1 selected");
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

        @SuppressLint("NewApi")
        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {

            switch (item.getItemId()) {
            case R.id.action_settings:
                Toast.makeText(getBaseContext(), "Selected Action1 ",
                        Toast.LENGTH_LONG).show();
                mode.finish();
                break;

            }
            return false;
           }

         };
       }

      }

长按期间我的自定义CAB显示没有文字选择 如何使它只出现在文本选择的情况下? 非常感谢

1 个答案:

答案 0 :(得分:0)

不要这样做,没有必要覆盖longClick,你应该制作自定义的webview,并在链接显示下面做一些事情:

Look At This Answer