Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在安卓上面制作一个小程序如何让他运转起来? #20

Open
13095180328 opened this issue Jan 10, 2025 · 0 comments
Open

Comments

@13095180328
Copy link

require "import"
import "android.app.Activity"
import "android.os.Bundle"
import "android.widget.TextView"
import "android.widget.Button"
import "android.widget.EditText"
import "android.view.View"
import "android.widget.ImageView"
import "math"

-- 创建骰子游戏Activity类
DiceGameActivity = Activity:extend{
onCreate = function(self, savedInstanceState)
self.super.onCreate(self, savedInstanceState)

    -- 设置布局
    self:setContentView(loadlayout{
        LinearLayout,
        orientation = "vertical",
        {
            EditText,
            id = "@+id/guessEdit",
            hint = "猜测总点数(6-36)",
            layout_width = "match_parent",
            layout_height = "wrap_content"
        },
        {
            Button,
            id = "@+id/rollButton",
            text = "掷骰子",
            layout_width = "wrap_content",
            layout_height = "wrap_content",
            onClick = "onRollButtonClick"
        },
        {
            TextView,
            id = "@+id/resultText",
            text = "结果将显示在这里",
            layout_width = "match_parent",
            layout_height = "wrap_content"
        }
    })

    -- 初始化骰子图像
    self.diceImages = {}
    for i = 1, 6 do
        self.diceImages[i] = self.findViewById(self, "@+id/dice" .. i)
    end
end

}

-- 掷骰子按钮点击事件
function onRollButtonClick(v)
local guess = tonumber(activity.findViewById(activity, "@+id/guessEdit").getText().toString())
if guess and guess >= 6 and guess <= 36 then
local total = 0
local results = {}
for i = 1, 6 do
local roll = math.random(1, 6)
total = total + roll
results[i] = roll
end
local resultText = activity.findViewById(activity, "@+id/resultText")
if total == guess then
resultText.setText("恭喜!你猜对了,总点数是 " .. total)
else
resultText.setText("很遗憾,总点数是 " .. total .. ",你的猜测是 " .. guess)
end
else
activity.findViewById(activity, "@+id/resultText").setText("请输入有效的点数(6-36)")
end
end

-- 应用入口点
function main()
local activity = DiceGameActivity()
activity.onCreate(nil)
end

-- 启动应用
main()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant