简体   繁体   English

如果方法返回true,则停止计时码表

[英]Stop a chronometer if a method returns true

Is there any way that the chronometer can be paused if a method is true. 如果方法为真,是否有任何方法可以使天文钟暂停。 I have created a simple jigsaw puzzle and added a chronometer to show the time elapse and I'm trying to stop the timer when the puzzle is Solved.On running, the application runs smoothly, with the chronometer ticking but not stopping when the game is solved. 我创建了一个简单的拼图游戏并添加了一个计时器以显示时间的流逝,并且在解决该难题时我试图停止计时器运行时,应用程序运行平稳,计时器在滴答作响,但在游戏结束时并未停止解决了。 Any sort of help will be appreciated. 任何帮助将不胜感激。 Here is the code; 这是代码;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu);
    chronometer  = findViewById(R.id.chronometer);

    stopChronometer();
    startChronometer();
    isSolved();
}

public static boolean isSolved() {
    boolean solved = false;
    for (int i = 0; i < tileList.length; i++) {
        if (tileList[i].equals(String.valueOf(i))) {
            solved = true;
        } else {
            solved = false;
            break;
        }
    }
    return solved;
}
//to start the chronometer
public void startChronometer(){
    if(!running){
        chronometer.setBase(SystemClock.elapsedRealtime());
        chronometer.start();
        running = true;
    }
}
//to stop the chronometer
public void stopChronometer(){
    if(running && isSolved()){
        chronometer.stop();
        running =false;
    }
}

If interested in the whole code; 如果对整个代码感兴趣;

import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import java.lang.*;
import java.util.ArrayList;
import java.util.Random;
import android.widget.Chronometer;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    //GestureDetectView* is the class where the puzzle grid is setup
    private static GestureDetectGridView mGridView;
    private static final int COLUMNS =3;
    private static final int DIMENSIONS = COLUMNS * COLUMNS;
    private static int mColumnWidth, mColumnHeight;
    //up, down, left, right are tile movements
    public static final String up = "up";
    public static final String down = "down";
    public static final String left = "left";
    public static final String right = "right";
    private static String[] tileList;

    private Chronometer chronometer;
    private boolean running;

    @Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        chronometer  = findViewById(R.id.chronometer);
        init();
        scramble();
        setDimensions();
        stopChronometer();
        startChronometer();
    }

    //to start the chronometer
    public void startChronometer(){
        if(!running){
            chronometer.setBase(SystemClock.elapsedRealtime());
            chronometer.start();
            running = true;
        }
    }
    //to stop the chronometer * PROBLEM AREA
    public void stopChronometer(){

    if(running && isSolved()){
        chronometer.stop();
        running =false;
        }
    }
    //Grid view from GestureDetectView class
    private void init() {
        mGridView = (GestureDetectGridView) findViewById(R.id.grid);
            mGridView.setNumColumns(COLUMNS);
        tileList = new String[DIMENSIONS];
        for (int i = 0; i < DIMENSIONS; i++) {
            tileList[i] = String.valueOf(i);
        }
    }
    //To shuffle the grid pieces
    private void scramble() {
        int index;
        String temp;
        Random random = new Random();

        for (int i = tileList.length -1; i > 0; i--) {
                index = random.nextInt(i + 1);
                temp = tileList[index];
                tileList[index] = tileList[i];
                tileList[i] = temp;
            }

        }
private void setDimensions() {
    ViewTreeObserver vto = mGridView.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            mGridView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            int displayWidth = mGridView.getMeasuredWidth();
            int displayHeight = mGridView.getMeasuredHeight();

            int statusbarHeight = getStatusBarHeight(getApplicationContext());
            int requiredHeight = displayHeight - statusbarHeight;

            mColumnWidth = displayWidth / COLUMNS;
            mColumnHeight = requiredHeight / COLUMNS;

            display(getApplicationContext());
                }
        });
    }

    private int getStatusBarHeight(Context context) {
        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen",
            "android");

        if (resourceId > 0) {
         result = context.getResources().getDimensionPixelSize(resourceId);
        }

        return result;
 }
    //To determine if puzzle is solved
    public static boolean isSolved() {
        boolean solved = false;
        for (int i = 0; i < tileList.length; i++)
        {
                if (tileList[i].equals(String.valueOf(i))) {
                solved = true;
            }   else {
                solved = false;
                break;
                }
        }
        return solved;
    }
    private static void swap(Context context, int Position, int swap) {
        String newPosition = tileList[Position + swap];
        tileList[Position + swap] = tileList[Position];
        tileList[Position] = newPosition;
        display(context);
        if (isSolved()) {
            Toast.makeText(context, "CONGRATULATIONS, YOU WIN!", Toast.LENGTH_SHORT).show();
        }
    }
//To source the image pieces and add them to the puzzle grid
    private static void display(Context context) {
        ArrayList<Button> buttons = new ArrayList<>();
        Button button;

        for (int i = 0; i < tileList.length; i++) {
            button = new Button(context);

            if (tileList[i].equals("0"))
                button.setBackgroundResource(R.drawable.piece1);
            else if (tileList[i].equals("1"))
                button.setBackgroundResource(R.drawable.piece2);
            else if (tileList[i].equals("2"))
                button.setBackgroundResource(R.drawable.piece3);
            else if (tileList[i].equals("3"))
                button.setBackgroundResource(R.drawable.piece4);
            else if (tileList[i].equals("4"))
                button.setBackgroundResource(R.drawable.piece5);
            else if (tileList[i].equals("5"))
                button.setBackgroundResource(R.drawable.piece6);
            else if (tileList[i].equals("6"))
                button.setBackgroundResource(R.drawable.piece7);
            else if (tileList[i].equals("7"))
                button.setBackgroundResource(R.drawable.piece8);
            else if (tileList[i].equals("8"))
                button.setBackgroundResource(R.drawable.piece9);

            buttons.add(button);

        }

        mGridView.setAdapter(new CustomAdapter(buttons, mColumnWidth, mColumnHeight));
    }

    public static void moveTiles(Context context, String direction, int position) {

        // Upper-left-corner tile
        if (position == 0) {

            if (direction.equals(right)) swap(context, position, 1);
            else if (direction.equals(down)) swap(context, position, COLUMNS);
            else Toast.makeText(context, "Invalid move", Toast.LENGTH_SHORT).show();

            // Upper-center tiles
        }   else if (position > 0 && position < COLUMNS - 1) {
            if (direction.equals(left)) swap(context, position, -1);
            else if (direction.equals(down)) swap(context, position, COLUMNS);
            else if (direction.equals(right)) swap(context, position, 1);
            else Toast.makeText(context, "Invalid move", Toast.LENGTH_SHORT).show();

            // Upper-right-corner tile
        }   else if (position == COLUMNS - 1) {
            if (direction.equals(left)) swap(context, position, -1);
            else if (direction.equals(down)) swap(context, position, COLUMNS);
            else Toast.makeText(context, "Invalid move", Toast.LENGTH_SHORT).show();

            // Left-side tiles
        } else if (position > COLUMNS - 1 && position < DIMENSIONS - COLUMNS &&
            position % COLUMNS == 0) {
            if (direction.equals(up)) swap(context, position, -COLUMNS);
            else if (direction.equals(right)) swap(context, position, 1);
            else if (direction.equals(down)) swap(context, position, COLUMNS);
            else Toast.makeText(context, "Invalid move", Toast.LENGTH_SHORT).show();

            // Right-side AND bottom-right-corner tiles
        } else if (position == COLUMNS * 2 - 1 || position == COLUMNS * 3 -1) 
          {
            if (direction.equals(up)) swap(context, position, -COLUMNS);
            else if (direction.equals(left)) swap(context, position, -1);
            else if (direction.equals(down)) {

            // Tolerates only the right-side tiles to swap downwards as opposed to the bottom-
            // right-corner tile.
            if (position <= DIMENSIONS - COLUMNS - 1) swap(context, position,
                    COLUMNS);
            else Toast.makeText(context, "Invalid move", Toast.LENGTH_SHORT).show();
            } else Toast.makeText(context, "Invalid move", Toast.LENGTH_SHORT).show();

            // Bottom-left corner tile
        } else if (position == DIMENSIONS - COLUMNS) {
            if (direction.equals(up)) swap(context, position, -COLUMNS);
            else if (direction.equals(right)) swap(context, position, 1);
            else Toast.makeText(context, "Invalid move", Toast.LENGTH_SHORT).show();

            // Bottom-center tiles
        } else if (position < DIMENSIONS - 1 && position > DIMENSIONS - COLUMNS) {
            if (direction.equals(up)) swap(context, position, -COLUMNS);
            else if (direction.equals(left)) swap(context, position, -1);
            else if (direction.equals(right)) swap(context, position, 1);
            else Toast.makeText(context, "Invalid move", Toast.LENGTH_SHORT).show();

            // Center tiles
        } else {
            if (direction.equals(up)) swap(context, position, -COLUMNS);
            else if (direction.equals(left)) swap(context, position, -1);
            else if (direction.equals(right)) swap(context, position, 1);
            else swap(context, position, COLUMNS);
        }
    }


    @Override
protected void onSaveInstanceState(Bundle outState){
    super.onSaveInstanceState(outState);


    }

}
public void stopChronometer() {
    if(running && isSolved()){
        chronometer.stop();
        running =false;
    }
}

change to 改成

public void stopChronometer(){
    if(running || isSolved()){
        chronometer.stop();
        running =false;
    }
}

or you can call stopChronometer() from isSolved() method (you have to remove isSolved() from stopChronometer though). 或者您可以从isSolved()方法中调用stopChronometer()(不过,您必须从stopChronometer中删除isSolved())。 Without seeing the complete code, it's hard to answer. 如果没有完整的代码,很难回答。

I have found the solution The issue whas that I hadn't declared the method start Chronometer(), chronomer and running as static. 我找到了解决方案问题是我没有声明方法start Chronometer(),chronomer和以静态方式运行。 Thanks for the help though😊 谢谢你的帮助😊

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM