-1

我在Android Studio中制作的小型计数器系统有问题,问题是我不知道如何做计数器加1.5,因为现在我一直按下加号按钮时它一直在做1那里,但在下面的脚本中必须是geld++;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);
    }
}
4

1 回答 1

0

首先,您需要更改IntDouble喜欢

double score = 0.00;

然后像这样递增/递减

递减

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);
        }
    });
于 2020-09-12T23:06:00.410 回答