In the first part, I described how to configure an Android smartphone to work with SL4A (Scripting Layer for Android), showed how to call system pop-up windows, get user-entered information from them and display the result of work in them. In the same part I will talk about building the interface to the application using WebViews . In short, WebViews are a way to build an application interface using HTML, JavaScript, and CSS. If we consider that in Android smartphones full-featured webkit, then building the interface is not such a problem task. # -*- coding: utf-8 -*- import android, os, sys, urllib2 from xml.etree import ElementTree def show_karma_value(value): return value droid = android.Android() droid.webViewShow(os.path.dirname(sys.argv[0]) + '/html/habr.html') # , - /html/habr.html while True: user = droid.eventWaitFor('login').result if user: droid.dialogCreateSpinnerProgress("", " ") # droid.dialogShow() try: feed = urllib2.urlopen('http://habrahabr.ru/api/profile/' + user['data']) # XML XML = ElementTree.XML(feed.read()) # XML try: # <error> XML.find("error").text value = ' ' except: # <error> , value = XML.find("karma").text except: value = ' ' droid.dialogDismiss() # droid.eventPost('show_karma_value', show_karma_value(value)) # show_karma_value droid.vibrate() # <html> <head> <title>Sensor Monitor</title> <style> .logo{ text-align: center; width: 110px; height: 110px; display: block; margin: 10px auto; background: url(http://habrahabr.ru/i/bg-multilogo.png) no-repeat 50% -144px; } #karma { color: #6DA3BD; } </style> </head> <body> <span class="logo"></span> <form onsubmit="speak(); return false;"> <label for="login"> %username%</label> <input type="text" id="login" /> <input type="submit" value="!" /> </form> <h2 id="karma" style="display: inline;" /></h2> <script> var droid = new Android(); var speak = function() { droid.eventPost("login", document.getElementById("login").value); } var show_karma_value = function(data) { document.getElementById("karma").innerHTML = ' ' + document.getElementById("login").value + ': ' + data.data; } droid.registerCallback("show_karma_value", show_karma_value); </script> </body> </html> var droid = new Android(); droid.registerCallback("show_karma_value", show_karma_value); droid.eventPost("login", document.getElementById("login").value); while True: user = droid.eventWaitFor('login').result droid.eventPost('show_karma_value', value) var show_karma_value = function(data) { document.getElementById("karma").innerHTML = ' ' + document.getElementById("login").value + ': ' + data.data; } 


Source: https://habr.com/ru/post/122281/
All Articles