2024/5/14

package com.example.team_work;
2
3import androidx.appcompat.app.AppCompatActivity;
4import android.os.Bundle;
5import android.view.View;
6import android.widget.Button;
7import android.widget.TextView;
8
9import java.util.Random;
10
11public class MainActivity extends AppCompatActivity {
12
13    private TextView resultTextView;
14    private Button drawButton;
15    private TextView remainingAttemptsTextView;
16    private String[] cards = {"动物", "植物"}; // 卡片数据硬编码
17    private int remainingAttempts = 5; // 初始化剩余抽奖次数为5
18
19    @Override
20    protected void onCreate(Bundle savedInstanceState) {
21        super.onCreate(savedInstanceState);
22        setContentView(R.layout.activity_main);
23
24        resultTextView = findViewById(R.id.resultTextView);
25        drawButton = findViewById(R.id.drawButton);
26        remainingAttemptsTextView = findViewById(R.id.remainingAttemptsTextView); // 初始化remainingAttemptsTextView
27
28        // 设置初始剩余次数
29        remainingAttemptsTextView.setText("剩余抽奖次数:" + remainingAttempts);
30
31        drawButton.setOnClickListener(new View.OnClickListener() {
32            @Override
33            public void onClick(View v) {
34                if (remainingAttempts > 0) { // 检查是否还有剩余次数
35                    drawCard();
36                    remainingAttempts--; // 抽卡后减少一次机会
37                    remainingAttemptsTextView.setText("剩余抽奖次数:" + remainingAttempts); // 更新UI显示的剩余次数
38                } else {
39                    resultTextView.setText("您的抽奖次数已用完!");
40                }
41            }
42        });
43    }
44
45    private void drawCard() {
46        Random random = new Random();
47        int cardIndex = random.nextInt(cards.length);
48        String drawnCard = cards[cardIndex];
49        resultTextView.setText("您抽到了:" + drawnCard);
50    }
posted @ 2024-05-14 22:10  几条小鱼  阅读(11)  评论(0)    收藏  举报