欢迎光临散文网 会员登陆 & 注册

Android QPython3 SL4A 选择函数 : DialogChoice.py

2020-09-15 16:07 作者:乘着船  | 我要投稿

#需要Dialog.py  https://www.bilibili.com/read/cv8654793


from Dialog import *


OCAN=('OK','Cancel','All','None')

OC=('OK','Cancel')

CC=('Continue','Cancel')

OCE=('OK','Cancel','Edit')

ABCD=('Apple','Banana','Cat','Dog')

PNNs={'positive':1,'negative':0,'neutral':None}

PNNn=(1,1,0,None)


import os

isfile=os.path.isfile

isdir=os.path.isdir

listdir=os.listdir


def OneChoice(title='Which is animal ?',message='Choose animal',list_=ABCD,default=None,returnValue=False,button=OC): #单选

    list_=list(list_)

    list_.insert(0,message)

    if default==None:

        default=0

    else:

        default+=1

    while True:

        jsla("dialogCreateAlert",title)

        jsla("dialogSetSingleChoiceItems",list_,default)

        ButtonText(button)

        jsla("dialogShow")

        try:

            w=Which()

        except:

            return None

        default=rsla("dialogGetSelectedItems")[0]

        if w==1:

            if default!=0:

                if returnValue:

                    return list_[default]

                else:

                    return default-1

            else:

                return ''

        else:

            return None

        

def MulChoice(title='Which are not fruit ?',message='Choose fruit',list_=ABCD,default=(),returnValue=False,button=OCAN): #多选

    All=tuple(range(1,len(list_)+1))

    list_=list(list_)

    list_.insert(0,message)

    AN=2

    if default==None:

        default=[]

    else:

        default=[i+1 for i in default]

    while True:

        jsla("dialogCreateAlert",title)

        jsla("dialogSetMultiChoiceItems",list_,default)

        ButtonText((button[0],button[1],button[AN]))

        jsla("dialogShow")

        try:

            w=Which()

        except:

            return None

        default=rsla("dialogGetSelectedItems")

        if w==1:

            rst=[]

            for i in default:

                if i:

                    if returnValue:

                        rst.append(list_[i])

                    else:

                        rst.append(i-1)

            return rst

        elif w==0:

            if AN==2:

                default=All

                AN=3

            else:

                default=()

                AN=2

        else:

            return None


def ynChoice(title='Math Problem',message='1+1=3 ?',button=YNC): #判断(是否)

    jsla("dialogCreateAlert",title, message)

    ButtonText(button)

    jsla("dialogShow")

    try:

        return PNNs[rsla("dialogGetResponse")['which']]

    except:

        return PNNn[len(button)]


def RunChoice(title='Choose to run',list_=('One','Mul','YN','Pause','Exit'),run=(OneChoice,MulChoice,'print(ynChoice())','input()',None)): #运行列表

    while True:

        a=List(title,list_)

        if a==None: #None : 退出选择菜单

            break

        a=run[a]

        if type(a)==str: #str类型 : 单句函数

            exec(a)

        elif a==None: #None : 退出选择菜单

            break

        else: #function : 执行function()

            a()


def FileChoice(title='选择一个文件(夹)',Path='/sdcard',FileName=''): #选择文件(夹)路径

    d=Path=PathStrip(Path)

    while True:

        try:

            l=listdir(d)

        except:

            l=[]

        D=[];F=[]

        l.sort()

        for i in l:

            j=d+i

            if isdir(j):

                D.append(d+i+'/')

            else:

                F.append(d+i)

        b=[]

        for i in D:

            b.append(i.rsplit('/',2)[-2]+':目录')

        for i in F:

            b.append(i.rsplit('/',1)[-1]+':文件')

        l=len(b)

        m=len(D)

        c=["<路径>%s\n<文件(夹)选择器功能键>"%d]

        c=List(title,c+b)

        if c==None:

            d=PathUp(d)

            continue

        elif c>0:

            c-=1

            if c<m:

                d=D[c]

                continue

            else:

                return F[c-m]

            continue

        c=List('选择要操作的功能',('选择该文件夹','向上一级文件夹','手动输入文件名','继续选择文件(夹)','回到初始路径','取消'))

        if c==0:

            return d

        elif c==1:

            d=PathUp(d)

        elif c==2:

            e=Input('输入文件名 :','当前路径 : '+d,FileName)

            if e!=None:

                return d+e

        elif c==4:

            d=Path

            continue

        elif c==5:

            return


def FileChoiceMessage(title='选择一个文件(夹)',message='默认路径:',Path='/sdcard',FileName='',button=OCE): #选择文件(夹)路径(默认路径)

    Path=PathStrip(Path)

    b=Path+FileName

    if message:

        c='%s\n%s'%(message,b)

    else:

        c=b

    a=Button(title,c,button)

    if a==1:

        return b

    elif a==0:

        pass

    else:

        return None

    return FileChoice(title,Path,FileName)


def PathUp(x): #请忽略

    y=x.rfind('/',0,-1)

    return x[:y+1]


def PathStrip(Path): #请忽略

    Path=Path.strip()

    if Path=='' or Path[-1]!='/':

        Path+='/'

    return Path


作者:乘着船@Bilibili

更多文章+下载链接:https://www.bilibili.com/read/readlist/rl321663


Android QPython3 SL4A 选择函数 : DialogChoice.py的评论 (共 条)

分享到微博请遵守国家法律