我试图制作一个简单的应用,在“屏幕一”上放置一个按钮,点击后打开一个网页。
我参考了这个主题(Python - Showing a web browser/iframe right into the app),但我不明白如何使用Michael提供的代码...
所以我尝试了这个... 当我启动由Buildozer构建的apk时,它没有工作:')
import kivy
kivy.require('1.9.2')
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
# MICHAEL'S CODE
from kivy.utils import platform
from kivy.uix.widget import Widget
from kivy.clock import Clock
from jnius import autoclass
from android.runnable import run_on_ui_thread
WebView = autoclass('android.webkit.WebView')
WebViewClient = autoclass('android.webkit.WebViewClient')
activity = autoclass('org.renpy.android.PythonActivity').mActivity
class Wv(Widget):
def __init__(self, **kwargs):
super(Wv, self).__init__(**kwargs)
Clock.schedule_once(self.create_webview, 0)
@run_on_ui_thread
def create_webview(self, *args):
webview = WebView(activity)
webview.getSettings().setJavaScriptEnabled(True)
wvc = WebViewClient();
webview.setWebViewClient(wvc);
activity.setContentView(webview)
webview.loadUrl('http://www.google.com/')
# END OF MICHAEL'S CODE
Builder.load_string('''
<ScreenOne>:
BoxLayout:
Label:
text: "SCREEN 1"
Button:
text: "GO GO GO TO GOOGLE !"
on_press: root.open_browser()
<ScreenTwo>:
BoxLayout:
Label:
text: "SCREEN 2"
Button:
text: "GO GO GO TO SCREEN 1"
on_press:
root.manager.transition.direction = "right"
root.manager.transition.duration = 1
root.manager.current = "screen_one"
''')
class ScreenOne(Screen):
def open_browser(self):
return Wv()
class ScreenTwo(Screen):
pass
screen_manager = ScreenManager()
screen_manager.add_widget(ScreenOne(name="screen_one"))
screen_manager.add_widget(ScreenTwo(name="screen_two"))
class BrowserApp(App):
def build(self):
return screen_manager
app = BrowserApp()
app.run()
应用程序没有崩溃,但在启动时就关闭了。
我做错了什么?我确定我没有正确使用它...
来自
adb logcat
的日志:
注:本文中保留HTML标记。06-13 12:54:47.559 7429 7510 I python : ImportError: No module named android
06-13 12:54:47.579 7429 7510 I python : Python for android ended.
adb logcat
了:https://developer.android.com/studio/command-line/logcat.html 没有人会在自己的设备上编译和运行它,这就是为什么您需要提供日志的原因。此外,Java类实际上还有文档:https://developer.android.com/reference/android/webkit/package-summary.html - Peter Badida