无法从webview获取选定的文本

时间:2015-04-23 09:34:33

标签: android webview

我正在尝试使用以下代码从webview中获取所选文本,但这返回null.please让我现在知道下面代码中的问题。

在此代码中index.html是我的sdcard / dheeraj / index.html文件夹中的html文件。

        import android.app.Activity;
        import android.content.res.AssetManager;
        import android.os.Bundle;
        import android.os.Environment;
        import android.content.ClipboardManager;
        import android.util.Log;
        import android.view.ActionMode;

        import android.view.KeyEvent;
        import android.view.Menu;
        import android.view.MenuItem;
        import android.webkit.WebSettings;
        import android.webkit.WebView;
        import android.widget.Toast;


    public class AddToNotesWithWebViewActivity extends Activity
    {

        AssetManager assetManager;
        WebView webview;
        String line, line1 = "", finalstr = "";
        int i = 0;
        private ActionMode mActionMode = null;
        private ClipboardManager clipboard;
        boolean mark_text=false;

        @Override
        public void onActionModeStarted(ActionMode mode) {
            if (mActionMode == null) {
                mActionMode = mode;
                Menu menu = mode.getMenu();
                // Remove the default menu items (select all, copy, paste, search)
                menu.clear();

                mode.getMenuInflater().inflate(R.menu.add_to_notes_menu, menu);
            }

            super.onActionModeStarted(mode);
        }


        public void onContextualMenuItemClicked(MenuItem item)
        {
            switch (item.getItemId())
            {
                case R.id.add_to_notes_menu_item:

                    clipboard =
                            (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
                    emulateShiftHeld(webview);
                    selectedText();




                    break;

                default:

                    break;
            }

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

        private void selectedText()
        {
            Toast.makeText(this, clipboard.getText(),
                    Toast.LENGTH_SHORT).show();
        }


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


        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.ac_add_to_notes_web_view);
            webview=(WebView)findViewById(R.id.web_view_browser);



            WebSettings webSettings = webview.getSettings();
            webSettings.setJavaScriptEnabled(true);

            webview.loadUrl("file://" + Environment.getExternalStorageDirectory() + "/dheeraj/index.html");

        }

        private void emulateShiftHeld(WebView view)
        {

         try
            {
                KeyEvent shiftPressEvent = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN,
                        KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0);
                shiftPressEvent.dispatch(view);

                registerForContextMenu(view);

            }
            catch (Exception e)
            {
                Log.e("dd", "Exception in emulateShiftHeld()", e);
            }
        }

    }

请帮忙.... ?????????

2 个答案:

答案 0 :(得分:1)

您可以参考以下代码:

 package com.Demo.SelectText;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.KeyEvent;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.webkit.WebView;

    public class MainActivity extends Activity {
     WebView mWebView;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            mWebView = (WebView)findViewById(R.id.webview);
            mWebView.loadUrl("http://devemat-androidprogramming.blogspot.com/");

        }

        private static final int SELECTTEXT_MENU_ID = Menu.FIRST;

        @Override
        public boolean onCreateOptionsMenu(Menu menu){
         super.onCreateOptionsMenu(menu);

         menu.add(0, SELECTTEXT_MENU_ID, 0, "Select Text");

         return true;
        }

        public boolean onPrepareOptionsMenu(Menu menu){
         super.onPrepareOptionsMenu(menu);
         return true;
        }

        public boolean onOptionsItemSelected(MenuItem item){
         switch(item.getItemId()){
         case SELECTTEXT_MENU_ID:
              SelectText();
              return true;
         }
         return true;
        }

        public void SelectText(){
        try{
          KeyEvent shiftPressEvent = 
                   new KeyEvent(0, 0, KeyEvent.ACTION_DOWN,     
                   KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0);
          shiftPressEvent.dispatch(mWebView);
      }catch(Exception e){
          throw new AssertionError(e);
      }
     }
    }

答案 1 :(得分:0)

        public void getMyText(String myText) {
          // this is called from JS
             System.out.println(myText);
       }

  }

     private class myWebViewClient extends WebViewClient {
            <at> Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
            }  


      Override
      public void onPageFinished(WebView view, String url) {
            view.loadUrl("javascript:window.cpjs.getMyText(document.getElementById('myText').innerHTML);void(0);");

        }
    }