Android 工作室柜台

Android Studio counter

我在 Android Studio 中制作的小型计数器系统有问题,问题是我不知道如何将计数器加 1.5,因为现在我一直按加号按钮它一直在那里 1 但在下面的脚本中必须是 1.5 geld++; 确保当你按下按钮时一直添加 1 效果很好但我不想要 1 但我一直想要 1.5,但我无法在任何地方找到解决方案。所以我希望有人能提供帮助。提前致谢!

package com.example.melkanalysetimer;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class wit extends AppCompatActivity {
    private TextView countdownText;
    private Button countdownButton;
    private Button krat;
    private Button remove;
    private TextView scoreb;
    private TextView money;
    private TextView reset;

    private CountDownTimer countDownTimer;
    private long timeLeftInMilliseconds = 960000;
    private boolean timerRunning;
    private static final long START_TIME_IN_MILLIS = 960000;

    int score = 0;
    int geld = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wit);

        countdownText = findViewById(R.id.countdown_text);
        countdownButton = findViewById(R.id.countdown_button);
        krat = findViewById(R.id.b_add);
        scoreb = findViewById(R.id.tv_score);
        remove = findViewById(R.id.b_remove);
        money = findViewById(R.id.ssgeld);
        reset = findViewById(R.id.button_reset);

        scoreb.setText("Kratjes: "+ score);
        money.setText("Geld: "+ geld);

        krat.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                score++;
                scoreb.setText("Kratjes: "+ score);
                geld++;
                money.setText("Geld: "+ geld);
            }
        });

        remove.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                score--;
                scoreb.setText("Kratjes: "+ score);
                geld--;
                money.setText("Geld: "+ geld);
            }
        });

        reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                resetTimer();
            }
        });

        countdownButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startStop();
            }
        });

        updateTimer();
    }

    public void startStop() {
        if (timerRunning) {
            stopTimer();
        } else {
            startTimer();
        }
    }

    public void startTimer() {
        countDownTimer = new CountDownTimer(timeLeftInMilliseconds, 1000) {
            @Override
            public void onTick(long l) {
                timeLeftInMilliseconds = l;
                updateTimer();
            }

            @Override
            public void onFinish() {

            }

        }.start();

        countdownButton.setText("PAUZE");
        timerRunning = true;
    }

    public void stopTimer() {
        countDownTimer.cancel();
        countdownButton.setText("START");
        timerRunning = false;
    }

    public void resetTimer() {
        timeLeftInMilliseconds = START_TIME_IN_MILLIS;
        updateTimer();
    }

    public void updateTimer() {
        int minutes = (int) timeLeftInMilliseconds / 60000;
        int seconds = (int) timeLeftInMilliseconds % 60000 / 1000;

        String timeLeftText;

        timeLeftText = "" + minutes;
        timeLeftText += ":";
        if (seconds < 10) timeLeftText += "0";
        timeLeftText += seconds;

        countdownText.setText(timeLeftText);
    }
}

首先你需要把Int改成Double

double score = 0.00;

然后Increment/Decrement像这样

递减

remove.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View view) {
        //check if score is less then 0 
        if(score > 1.5){
          score = score - 1.5;
        }
        scoreb.setText("Kratjes: "+ score);
        geld--;
        money.setText("Geld: "+ geld);
    }
});

增量

krat.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            score = score + 1.5;
            scoreb.setText("Kratjes: "+ score);
            geld++;
            money.setText("Geld: "+ geld);
        }
    });