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):#主函数
#复选按钮组(标题,复选按钮文本组(按钮1,按钮2,……),返回数值,(确认按钮文本,复制按钮文本,取消按钮文本),(全选按钮文本,全不选按钮文本))
#返回:[选择内容的list列表];如果不选按确定,返回空列表[];如果取消,返回None。
#返回数值(returnValue)为True,返回实际值;返回数值(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')
==========说明==========

此时如果按“确定”,返回:[],
此时如果按下“全对”按钮,

此时如果按“确定”,返回:[0,1,2],
如果returnValue=True,返回['答案1','答案2','答案3']。
作者:乘着船@Bilibili
更多文章+下载链接:https://www.bilibili.com/read/readlist/rl321663