2011-12-21 22 views

答えて

2

あなたはWebViewののjavascriptの方法を実行することができます:要素IDを取得するにはjavascriptの

方法、開始を位置と終了位置

WebViewの上で実行される
function(){ 
var html = ""; 
if (typeof window.getSelection != "undefined") { 
    var sel = window.getSelection(); 
} 
return  sel.anchorNode.parentElement.id+':'+sel.anchorOffset+':'+sel.focusOffset; 
})(); 

webview.evaluateJavascript(script, new ValueCallback<String>() { 
    @Override 
    public void onReceiveValue(String s) { 
     selectedData = s; //value of javascript return 
     selectedData = selectedData.replaceAll("\"", ""); 
     String array[] = new String[3]; 
     elementId = array[0]; 
     startPosition=array[1]; 
     endPosition=array[2]; 
     Log.d("WebView runtime", selectedText); 
    } 
}); 

を、あなたは、この選択の使用のテキストを取得したい場合

function(){ 
var html = ""; 
if (typeof window.getSelection != "undefined") { 
    var sel = window.getSelection(); 
} 
return sel.text; 
})(); 

カスタムコールバックを使用する必要がある場合は、カスタムのWebViewを作成する必要があります

private class TesteCallback implements Callback { 

    private MyWebView mywebview; 

    private TextMark textmark; 

    public TesteCallback(MyWebView mywebview) { 
     this.mywebview = mywebview; 

    } 

    @Override 
    public boolean onCreateActionMode(ActionMode mode, Menu menu) { 

     mode.setTitle("Chose color"); 

     mode.getMenuInflater().inflate(R.menu.pagina2, menu); 

     return true; 
    } 

    /** 
    * create custom itens, remove useless itens 
    */ 
    @Override 
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) { 
     menu.removeItem(android.R.id.selectAll); 
     // Remove the "cut" option 
     menu.removeItem(android.R.id.cut); 
     // Remove the "copy all" option 
     menu.removeItem(android.R.id.copy); 

     return true; 
    } 
@Override 
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) { 
     mywebview.pegarSelecao(); 

     textmark = new TextMark(); 
     switch (item.getItemId()) { 
     case R.id.red: 
      // do some stuff 

      break; 
     case R.id.yellow: 
      // do some stuff 

      break; 
     case R.id.blue: 
      // do some stuff 

      break; 
     default: 

      break; 
     } 

     return false; 
    } 
}//end class 

カスタムコールバックを作成することができ、このメソッドの呼び出しとコールバックを呼び出して実行する:

public class MyWebView extends WebView { 
public MyWebView(Context context) { 

    super(context); 
    this.contexto = context; 

} 
public MyWebView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    // TODO Auto-generated constructor stub 
} 

public MyWebView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    // TODO Auto-generated constructor stub 
} 
private ActionMode.Callback mActionModeCallback; 

/////////////////////////////////////////// 
@Override 
public ActionMode startActionMode(Callback callback) { 
    ViewParent parent = getParent(); 
    if (parent == null) { 
     return null; 
    } 

    mActionModeCallback = new TesteCallback(this); //this line call custom callback 
    return parent.startActionModeForChild(this, mActionModeCallback); 
} 

} 
関連する問題