कदम: 1 कस्टम WebView वर्ग बनाएँ। यह कक्षा वेबव्यू टेक्स्ट पर लंबे समय तक प्रेस पर मूल क्रिया पट्टी को ओवरराइड कर देगी। इसके अलावा यह एंड्रॉइड के विभिन्न संस्करण (4.0 पर परीक्षण) के लिए चयन केस को संभालता है यह कोड जावास्क्रिप्ट का उपयोग करके चयनित टेक्स्ट लेता है।
public class CustomWebView extends WebView {
private Context context;
// override all other constructor to avoid crash
public CustomWebView(Context context) {
super(context);
this.context = context;
WebSettings webviewSettings = getSettings();
webviewSettings.setJavaScriptEnabled(true);
// add JavaScript interface for copy
addJavascriptInterface(new WebAppInterface(context), "JSInterface");
}
// setting custom action bar
private ActionMode mActionMode;
private ActionMode.Callback mSelectActionModeCallback;
private GestureDetector mDetector;
// this will over ride the default action bar on long press
@Override
public ActionMode startActionMode(Callback callback) {
ViewParent parent = getParent();
if (parent == null) {
return null;
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
String name = callback.getClass().toString();
if (name.contains("SelectActionModeCallback")) {
mSelectActionModeCallback = callback;
mDetector = new GestureDetector(context,
new CustomGestureListener());
}
}
CustomActionModeCallback mActionModeCallback = new CustomActionModeCallback();
return parent.startActionModeForChild(this, mActionModeCallback);
}
private class CustomActionModeCallback implements ActionMode.Callback {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mActionMode = mode;
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.copy:
getSelectedData();
mode.finish();
return true;
case R.id.share:
mode.finish();
return true;
default:
mode.finish();
return false;
}
}
@Override
public void onDestroyActionMode(ActionMode mode) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
clearFocus();
}else{
if (mSelectActionModeCallback != null) {
mSelectActionModeCallback.onDestroyActionMode(mode);
}
mActionMode = null;
}
}
}
private void getSelectedData(){
String js= "(function getSelectedText() {"+
"var txt;"+
"if (window.getSelection) {"+
"txt = window.getSelection().toString();"+
"} else if (window.document.getSelection) {"+
"txt = window.document.getSelection().toString();"+
"} else if (window.document.selection) {"+
"txt = window.document.selection.createRange().text;"+
"}"+
"JSInterface.getText(txt);"+
"})()";
// calling the js function
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
evaluateJavascript("javascript:"+js, null);
}else{
loadUrl("javascript:"+js);
}
}
private class CustomGestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onSingleTapUp(MotionEvent e) {
if (mActionMode != null) {
mActionMode.finish();
return true;
}
return false;
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// Send the event to our gesture detector
// If it is implemented, there will be a return value
if(mDetector !=null)
mDetector.onTouchEvent(event);
// If the detected gesture is unimplemented, send it to the superclass
return super.onTouchEvent(event);
}
}
चरण 2: WebView इंटरफ़ेस के लिए अलग वर्ग पैदा करते हैं। घटना के लिए इस वर्ग listnes जावास्क्रिप्ट एक बार से कोड निष्पादित हो रही है
public class WebAppInterface {
Context mContext;
WebAppInterface(Context c) {
mContext = c;
}
@JavascriptInterface
public void getText(String text) {
// put selected text into clipdata
ClipboardManager clipboard = (ClipboardManager)
mContext.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("simple text",text);
clipboard.setPrimaryClip(clip);
// gives the toast for selected text
Toast.makeText(mContext, text, Toast.LENGTH_SHORT).show();
}
}
चरण 3: मेनू जोड़ें। तुम लोगों के लिए धन्यवाद: रेस में कस्टम मेनू के लिए एक्सएमएल> मेनू फ़ोल्डर
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/copy"
android:icon="@drawable/ic_action_copy"
android:showAsAction="always"
android:title="copy">
</item>
<item
android:id="@+id/share"
android:icon="@drawable/ic_action_share"
android:showAsAction="always"
android:title="share">
</item>
मैं नीचे सूचीबद्ध इस लक्ष्य को हासिल करने के लिए कई लिंक की मदद ली।
कैसे संस्करण 4.0 के लिए डिफ़ॉल्ट कार्रवाई बार How to override default text selection of android webview os 4.1+?
अधिभावी के लिए जावास्क्रिप्ट Why can't I inject this javascript in the webview on android?
इंजेक्शन लगाने के लिए वेबव्यू http://developer.android.com/guide/webapps/webview.html#UsingJavaScript
पर जावास्क्रिप्ट का उपयोग करने के। 4.3 पाठ चयन करने के लिए Webview text selection not clearing
कृपया पूरा टुकड़ा –