本文介绍了python sl4a中的文件对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在sl4a中寻找一个简单的文件选择器对话框.我在此处找到了一些本机对话框,但没有找到我在找.

I am looking for an simple file chooser dialog in sl4a. I have found a few native dialogs here but did not find the one I was looking for.

我希望我可以通过找到容易获得的东西来节省时间.像filename = fileopendialog()这样的最小代码将是一个好处.

I wish I could save time by finding something readily available. A minimal code like filename = fileopendialog() would be a bonus.

有什么想法吗?

推荐答案

我决定写自己的(请参阅下面的参考资料).可能会做得更好,欢迎提出任何建议.

I decided to write my own (see below for reference). This could probably be made better, any suggestions welcome.

import android, os, time

droid = android.Android()
# Specify root directory and make sure it exists.
base_dir = '/sdcard/sl4a/scripts/'
if not os.path.exists(base_dir): os.makedirs(base_dir)

def show_dir(path=base_dir):
    """Shows the contents of a directory in a list view."""
    #The files and directories under "path"
    nodes = os.listdir(path)
    #Make a way to go up a level.
    if path != base_dir: 
        nodes.insert(0, '..')
    droid.dialogCreateAlert(os.path.basename(path).title())
    droid.dialogSetItems(nodes)
    droid.dialogShow()
    #Get the selected file or directory .
    result = droid.dialogGetResponse().result
    droid.dialogDismiss()
    if 'item' not in result:
       return
    target = nodes[result['item']]
    target_path = os.path.join(path, target)
    if target == '..': target_path = os.path.dirname(path)
    #If a directory, show its contents .
    if os.path.isdir(target_path): 
        show_dir(target_path)
    #If an file display it.
    else:
        droid.dialogCreateAlert('Selected File','{}'.format(target_path))
        droid.dialogSetPositiveButtonText('Ok')
        droid.dialogShow()
        droid.dialogGetResponse()

if __name__ == '__main__':
    show_dir()

这篇关于python sl4a中的文件对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 06:48