首页 > 解决方案 > 在kivy中关闭webview

问题描述

我试图关闭或销毁 webview 并显示其他布局这是我的 webview 代码,或者如果这不可能,我想获取当前页面的链接,如果当前页面链接等于某个链接,我想退出webview请帮我解决这个问题

from android.runnable import run_on_ui_thread as run_thread
from jnius import autoclass
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.app import runTouchApp
from kivy.clock import Clock, mainthread
@mainthread
def quit_screen():
    app = App.get_running_app()
    e=GridLayout()
    d1=button(text="hi end")
    e.add_widget(e)
    app.root.switch_screen(e)
@run_thread
def key_back_handler():
    webview.loadUrl("about:blank")
    webview.clearHistory()
    webview.clearCache(True)
    webview.clearFormData()
    webview.freeMemory()
    Clock.schedule_once(quit_screen, 0)
@run_thread
def WebView(link,*args):
    WebV = autoclass('android.webkit.WebView')
    WebViewClient = autoclass('android.webkit.WebViewClient')
    activity = autoclass('org.kivy.android.PythonActivity').mActivity

    webview = WebV(activity)
    settings = webview.getSettings()
    settings.setJavaScriptEnabled(True)
    settings.setUseWideViewPort(True) 
    settings.setLoadWithOverviewMode(True) 
    settings.setSupportZoom(True)
    settings.setBuiltInZoomControls(True)
    wvc = WebViewClient()
    webview.setWebViewClient(wvc)
    activity.setContentView(webview)
    webview.loadUrl(link)

m=GridLayout(cols=1,rows=1)
d=GridLayout(cols=1,rows=1)
m.add_widget(d)
def Push(butoon):
    WebView("https://www.google.com")
b=Button(text="hi")
b.bind(on_press=Push)
d.add_widget(b)


runTouchApp(m).run()

像这样,但它没有在 android 上按下后关闭喜欢这张图片

提前感谢您的帮助

标签: pythonandroidwebviewkivy

解决方案


诀窍是将 Webview 放在 Kivy ModalView 中并从 Java 中捕获后退按钮/手势以退出 ModalView ,例如:

https://github.com/RobertFlatt/Android-for-Python/tree/main/webview


推荐阅读