2016-12-03 3 views
0

WebView Androidでテキスト選択が変更されたときにユーザーに通知しようとしています。WebView androidの選択テキストの変更を聞く

私が使用して、setOnTouchListenerを使用して

android.view.MotionEvent.ACTION_UP 

に耳を傾け、タッチアップ後に新しいものと最後の選択テキストを比較してみました:

webview.loadUrl("javascript:js.callback(window.getSelection().toString())"); 

webSettings.setJavaScriptEnabled(true); 
myWebView.addJavascriptInterface(new WebAppInterface(), "Android"); 
public class WebAppInterface { 
     @JavascriptInterface 
     public void callback(String value) { 
      Log.d(getClass().getName(), value); 
      selected = value; 
      if (selected.length() > 0) { 
       Snackbar.make(findViewById(android.R.id.content), selected, Snackbar.LENGTH_SHORT).show(); 
      } 
     } 
} 

をしかし、私ユーザーが選択カーソルをドラッグ/変更すると、OnTouchが発火しないd

答えて

0

問題を解決するのに十分な解決策に達しましたが、カーソルをドラッグするとonTouchがなぜ起動されないのかまだ分かりません。ここ

は、私はすべての1000ミリ秒を選択テキストをチェックするsetOnInterval()を使用し、変更があれば、それはWebAppInterface

myWebView = (WebView) findViewById(R.id.webview); 
WebSettings webSettings = myWebView.getSettings(); 
webSettings.setJavaScriptEnabled(true); 
myWebView.addJavascriptInterface(new WebAppInterface(), "Android"); 
myWebView.loadData("Logcat is a tool that dumps a log of system messages. The messages include a stack trace when the device throws an error, as well as log messages written from your application and those written using JavaScript console APIs" + 
     "<script>" + 
     "var text='';setInterval(function(){ if(window.getSelection().toString() && text!==window.getSelection().toString()){text=window.getSelection().toString();console.log(text);Android.showToast(text); }}, 1000);" + 
     "</script>" +,"text/html; charset=UTF-8", null); 
myWebView.setWebChromeClient(new WebChromeClient() { 
    public void onConsoleMessage(String message, int lineNumber, String sourceID) { 
     Log.d("MyApplication", message); 
    } 
}); 

public class WebAppInterface { 
    /** Show a toast from the web page */ 
    @JavascriptInterface 
    public void showToast(String toast) { 
     Toast.makeText(MainActivity.this, toast, Toast.LENGTH_SHORT).show(); 
    } 
} 
に火
関連する問題