Android QPython3 可視化-多選:CheckGroup.py
#需要BaseWindow.py??https://www.bilibili.com/read/cv8680125
from BaseWindow import *
CB="""
<CheckBox
? ? android:id="@+id/Check%s"
? ? android:layout_width="fill_parent"
? ? android:layout_height="wrap_content"
? ? android:textSize="8dp"
? ? android:text="%s"
? ? android:background="#%s"
/>
"""
XML="""<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#4f3f2f"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="20">
<ScrollView? ?
? ? ? ? android:layout_width="fill_parent"? ?
? ? ? ? android:layout_height="fill_parent" >?
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="20">
? ? <EditText
android:id="@+id/Title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="8dp"
android:text="%s"
android:textColor="#ff0000"
android:layout_weight="1"
android:gravity="center"
/>
""","""
</LinearLayout>
</ScrollView>
? ? </LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="100dp"
android:orientation="horizontal"
android:layout_weight="8">
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="%s"
android:id="@+id/but_conf"
android:textSize="6dp"
android:background="#007f7f"
android:textColor="#ffffffff"
android:layout_weight="1"
android:gravity="center"/>
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="%s"
android:id="@+id/but_copy"
android:textSize="6dp"
android:background="#7f7f00"
android:textColor="#ffffffff"
android:layout_weight="1"
android:gravity="center"/>
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="%s"
android:id="@+id/but_canc"
android:textSize="6dp"
android:background="#7f007f"
android:textColor="#ffffffff"
android:layout_weight="1"
android:gravity="center"/>
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="加載中…"
android:id="@+id/but_alno"
android:textSize="6dp"
android:background="#3f3f3f"
android:textColor="#ffffffff"
android:layout_weight="1"
android:gravity="center"/>
</LinearLayout>
</LinearLayout>"""
def getCheck(self):
? ? j=[]
? ? C=self.Check
? ? c=self.check
? ? for i in range(self.Count):
? ? ? ? if c[i].checked=='true':
? ? ? ? ? ? if C:
? ? ? ? ? ? ? ? j.append(C[i])
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? j.append(i)
? ? return j
class MainScreen(Layout):
? ? def on_show(self):
? ? ? ? v=self.views
? ? ? ? Check=[]
? ? ? ? for i in range(self.Count):
? ? ? ? ? ? Check.append(v.pop('Check%s'%i))
? ? ? ? self.check=Check
? ? ? ? v.but_alno.text=self.AlNo[0]
? ? ? ? v.but_conf.add_event(click_EventHandler(v.but_conf,self.conf))
? ? ? ? v.but_copy.add_event(click_EventHandler(v.but_copy,self.copy))
? ? ? ? v.but_alno.add_event(click_EventHandler(v.but_alno,self.alno))
? ? ? ? v.but_canc.add_event(click_EventHandler(v.but_canc,self.canc))
? ? def on_close(self):
? ? ? ? pass
? ? def copy(self,view,dummy):
? ? ? ? droid.setClipboard(str(getCheck(self)))
? ? def alno(self,view,dummy):
? ? ? ? if self.views.but_alno.text==self.AlNo[0]:
? ? ? ? ? ? a=self.AlNo[1];b='true'
? ? ? ? else:
? ? ? ? ? ? a=self.AlNo[0];b='false'
? ? ? ? self.views.but_alno.text=a
? ? ? ? c=self.check
? ? ? ? for i in range(self.Count):
? ? ? ? ? ? c[i].checked=b
? ? def conf(self,view,dummy):
? ? ? ? MainScreen.Text=getCheck(self)
? ? ? ? FullScreenWrapper2App.close_layout()
? ? def canc(self,view,dummy):
? ? ? ? MainScreen.Text=None
? ? ? ? FullScreenWrapper2App.close_layout()
ALNO=('全選','全不選')
rcColor=('7f3f3f','3f3f7f')
def CheckGroup(Title='',Check=(),returnValue=False,ycnButton=CCC,anButton=ALNO):#主函數(shù)
#復(fù)選按鈕組(標(biāo)題,復(fù)選按鈕文本組(按鈕1,按鈕2,……),返回?cái)?shù)值,(確認(rèn)按鈕文本,復(fù)制按鈕文本,取消按鈕文本),(全選按鈕文本,全不選按鈕文本))
#返回:[選擇內(nèi)容的list列表];如果不選按確定,返回空列表[];如果取消,返回None。
#返回?cái)?shù)值(returnValue)為True,返回實(shí)際值;返回?cái)?shù)值(returnValue)為False,返回序號。
? ? r=[XML[0]%Str2Xml(Title)];i=-1
? ? for i in range(len(Check)):
? ? ? ? r.append(CB%(i,Str2Xml(Check[i]),rcColor[i%2]))
? ? MainScreen.Count=i+1
? ? if returnValue:
? ? ? ? MainScreen.Check=Check
? ? else:
? ? ? ? MainScreen.Check=False
? ? r.append(XML[1]%tuple(ycnButton))
? ? r=''.join(r)
? ? MainScreen.AlNo=anButton
? ? FullScreenWrapper2App.show_layout(MainScreen(r,'Check'))
? ? FullScreenWrapper2App.eventloop()
? ? r=MainScreen.Text
? ? del MainScreen.Text,MainScreen.AlNo
? ? return r
__all__=('CheckGroup','droid')
==========說明==========

此時(shí)如果按“確定”,返回:[],
此時(shí)如果按下“全對”按鈕,

此時(shí)如果按“確定”,返回:[0,1,2],
如果returnValue=True,返回['答案1','答案2','答案3']。
作者:乘著船@Bilibili
更多文章+下載鏈接:https://www.bilibili.com/read/readlist/rl321663