首页 > 解决方案 > 循环设置点击按钮事件

问题描述

在我的应用程序中,有几个按钮具有相同的操作:

when button1.Click do ...
when button2.Click do ...
when button3.Click do ...
when button4.Click do ...
... 

我可以在循环中为它们设置点击事件吗?

标签: app-inventor

解决方案


您可以在每个按钮单击事件中调用相同的过程并将按钮 ID 作为输入参数传递

when button1.Click do 
  call myProcedure "1"...

when button2.Click do 
  call myProcedure "2"...

when button3.Click do 
  call myProcedure "3"...

when button4.Click do 
  call myProcedure "4"...

然后在 for each 循环中,您可以执行类似的操作(假设有 4 个按钮)并将当前数字传递给过程

for each number from 1 to 4 do
  call procedure "number"

学习 App Inventor 的一个很好的方法是阅读 AI2 免费在线电子书 http://www.appinventor.org/book2中的免费 Inventor's Manual ...链接在网页底部。这本书“教”用户如何使用 AI2 块进行编程。这里有一个免费的编程课程http://www.appinventor.org/content/CourseInABox/Intro 和书中项目的 aia 文件在这里: http://www.appinventor.org/bookFiles
如何做一个此处描述了 App Inventor 的许多基本内容:http: //www.appinventor.org/content/howDoYou/eventHandling

还可以通过教程http://appinventor.mit.edu/explore/ai2/tutorials.html来学习 App Inventor 的基础知识,然后尝试一些东西并遵循Top 5 Tips: How to learn App Inventor


推荐阅读