我有一个自定义网络视图。
class NewWebView : WebView {
private val name = "Android"
private var mOnScrollChangedCallback: OnScrollChangedCallback? = null
private var onJSEventListener: OnJSEventListener? = null
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
prepareWebView()
}
constructor(context: Context?) : super(context) {
prepareWebView()
}
constructor(context: Context?, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle) {
prepareWebView()
}
@SuppressLint("SetJavaScriptEnabled")
private fun prepareWebView() {
addJavascriptInterface(WebAppInterface(), name)
settings.javaScriptEnabled = true
settings.domStorageEnabled = true
settings.javaScriptCanOpenWindowsAutomatically = true
webViewClient = NewWebViewClient()
}
fun setOnJSEventListener(onJSEventListener: OnJSEventListener?) {
this.onJSEventListener = onJSEventListener
}
fun loadPage(url: String?) {
val headerMap: MutableMap<String, String> = HashMap()
headerMap["My-App"] = "android"
loadUrl(url, headerMap)
}
override fun onScrollChanged(l: Int, t: Int, oldl: Int, oldt: Int) {
super.onScrollChanged(l, t, oldl, oldt)
if (mOnScrollChangedCallback != null) mOnScrollChangedCallback!!.onScroll(l, t)
}
private var contentHeight = 0
override fun getContentHeight(): Int {
if (contentHeight == 0) contentHeight = computeVerticalScrollRange()
return super.getContentHeight()
}
fun setOnScrollChangedCallback(onScrollChangedCallback: OnScrollChangedCallback?) {
mOnScrollChangedCallback = onScrollChangedCallback
}
interface OnScrollChangedCallback {
fun onScroll(l: Int, t: Int)
}
internal inner class WebAppInterface {
@JavascriptInterface
fun finish() {
if (onJSEventListener != null) {
onJSEventListener!!.onJSEvent("JS_FINISH")
}
}
}
interface OnJSEventListener {
fun onJSEvent(event: String?)
fun onJSEvent(event: String?, param: String?)
}
open class NewWebViewClient : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
val headerMap: MutableMap<String, String> = HashMap()
headerMap[" My-App"] = "android"
view?.let { webView ->
request?.let {
webView.loadUrl(it.url.toString(), headerMap)
}
}
return true
}
}
}
当我在活动中使用webwiew时,复制和粘贴的上下文菜单会出现,这样我就可以从webview复制文本。但是,当我在服务上使用相同的webview时,上下文菜单不会出现。我看到其他人在stackoverflow上也遇到了这个问题,但仍然没有解决方案。
在服务上使用webview时,如何在webview上启用文本选择?
暂无答案!
目前还没有任何答案,快来回答吧!