0

私はAndroidでPythonを初めて使用し、PythonスクリプトがWeb ページと通信します (Web ページにテキストを入力すると、Python スクリプトがそれを処理し、表示のために Web ページに送り返します)。これを行う私の試みでは、python スクリプトはページを開くことができますが、Web ページとスクリプトの間にダイアログはありません。Web ページの「 var android = new Android() 」という行が例のように機能しないようです。

ウェブページのコードは次のとおりです。

<!DOCTYPE HTML>
<html>
    <head>
        <script>
            var droid = new Android();

            function postInput(input) {
                if (event.keyCode == 13)
                    droid.eventPost('line', input)

                droid.registerCallback('stdout', function(e) {
                    document.getElementById('output').innerHTML = e.data;
                });
            }
        </script>   
    </head> 
    <body>
        <div id="banner">
            <h1>SL4A Webviews</h1>
            <h2>Example: Python Evaluator</h2>
        </div>

        <input id="userin" type="text" spellcheck="false"
               autofocus="autofocus" onkeyup="postInput(this.value)"
        />

        <div id="output"></div>

        <button id="killer" type="button"
                onclick="droid.eventPost('kill', '')"
        >QUIT</button>
    </body>
</html>

上記の Web ページを起動する Python コード:

import sys, androidhelper
droid = androidhelper.Android()

def line_handler(line):
    ''' Evaluate user input and print the result into a webview.
    This function takes a line of user input and calls eval on
    it, posting the result to the webview as a stdout event.
    '''
    output = str(eval(line))
    droid.eventPost('stdout', output)

droid.webViewShow('file:///storage/emulated/0/com.hipipal.qpyplus/scripts/webview1.html')

while True:
    event = droid.eventWait().result

    if event['name'] == 'kill':
        sys.exit()
    elif event['name'] == 'line':
        line_handler(event['data'])

Web ページの Android() インスタンスがどのように機能するはずだったのか、本当にわかりません。助けてくれてありがとう!(AndroidロリポップでSL4Aライブラリを使用してqpythonを実行しています)

4

1 に答える 1