Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 当if语句为真时打开活动_Android - Fatal编程技术网

Android 当if语句为真时打开活动

Android 当if语句为真时打开活动,android,Android,嗨,我想在if语句实现时打开一个活动。如“如果gameStatus等于12,则打开scoreActivity”。守则: import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.GridLa

嗨,我想在if语句实现时打开一个活动。如“如果gameStatus等于12,则打开scoreActivity”。守则:

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.GridLayout;
import java.util.Random;
import android.os.Build;
import android.os.Handler;

public class Game6x4Activity extends AppCompatActivity implements View.OnClickListener {

    private int numberOfElements;
    private int[] buttonGraphicLocations;
    private MemoryButton selectedButton1;
    private MemoryButton selectedButton2;
    private boolean isBusy = false;
    public int gameStatus;
    public int gameScore;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_mode);

        gameScore = 0;
        gameStatus = 0;

        GridLayout gridLayout = (GridLayout)findViewById(R.id.grid_layout_6x4);

        int numColumns = gridLayout.getColumnCount();
        int numRow = gridLayout.getRowCount();

        numberOfElements = numColumns * numRow;

        MemoryButton[] buttons = new MemoryButton[numberOfElements];

        int[] buttonGraphics = new int[numberOfElements / 2];

        buttonGraphics[0] = R.drawable.card1;
        buttonGraphics[1] = R.drawable.card2;
        buttonGraphics[2] = R.drawable.card3;
        buttonGraphics[3] = R.drawable.card4;
        buttonGraphics[4] = R.drawable.card5;
        buttonGraphics[5] = R.drawable.card6;
        buttonGraphics[6] = R.drawable.card7;
        buttonGraphics[7] = R.drawable.card8;
        buttonGraphics[8] = R.drawable.card9;
        buttonGraphics[9] = R.drawable.card10;
        buttonGraphics[10] = R.drawable.card11;
        buttonGraphics[11] = R.drawable.card12;

        buttonGraphicLocations = new int[numberOfElements];

        shuffleButtonGraphics();

        for(int r=0; r < numRow; r++)
        {
            for(int c=0; c <numColumns; c++)
            {
                MemoryButton tempButton = new MemoryButton(this, r, c, buttonGraphics[buttonGraphicLocations[r * numColumns + c]]);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                    tempButton.setId(View.generateViewId());
                }
                tempButton.setOnClickListener(this);
                buttons[r * numColumns + c] = tempButton;
                gridLayout.addView(tempButton);
            }
        }
    }
    protected void shuffleButtonGraphics(){
        Random rand = new Random();

        for (int i=0; i < numberOfElements; i++)
        {
            buttonGraphicLocations[i] = i % (numberOfElements / 2);
        }
        for (int i=0; i < numberOfElements; i++)
        {
            int temp = buttonGraphicLocations[i];

            int swapIndex = rand.nextInt(16);

            buttonGraphicLocations[i] = buttonGraphicLocations[swapIndex];

            buttonGraphicLocations[swapIndex] = temp;
        }
    }

    private int buttonGraphicLocations(int i) {
        return 0;
    }

    @Override
    public void onClick(View view) {

        if(isBusy) {
            return;
        }

        MemoryButton button = (MemoryButton) view;

        if(button.isMatched) {
            return;
        }

        if(selectedButton1 == null)
        {
            selectedButton1 = button;
            selectedButton1.flip();
            return;
        }

        if(selectedButton1.getId()== button.getId())
        {
            return;
        }


        if (selectedButton1.getFrontDrawableId()== button.getFrontDrawableId())
        {
            button.flip();

            button.setMatched(true);

            if (selectedButton1 != null) {
                selectedButton1.setEnabled(false);
                System.out.println("not null");
            }
            else{
                System.out.println("null");
            }
            if (selectedButton2 != null) {
                selectedButton2.setEnabled(false);
                System.out.println("not null");
            }
            else{
                System.out.println("null");
            }

            gameStatus = gameStatus + 1;
            gameScore = gameScore + 10;

            if (gameStatus == 12){
                Intent it = new Intent(Game6x4Activity.this, ActivityScore.class);
                startActivity(it);
            }

            selectedButton1 = null;

            return;
        }
        else
       {
            selectedButton2 = button;
            selectedButton2.flip();
            isBusy = true;

            final Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                public void run(){
                    selectedButton2.flip();
                    selectedButton1.flip();
                    selectedButton1 = null;
                    selectedButton2 = null;
                    isBusy = false;
                }
            },500);
            return;
       }
    }
}
" 有4种游戏模式:这是6x4游戏,我们可以找到24张牌(12张图片)

我想,你是在要求这样做。你可以通过

intent.putExtra("key",desired value);

如果(gameStatus==12){Intent it=new Intent(Game6x4Activity.this,ActivityScore.class);startActivity(it);}我必须传递给另一个activity游戏运行良好,直到gameStatus达到12,当它发生时,游戏返回到主标题。。。
else if (gameStatus == 15){
 Intent intent = new Intent(Game6x4Activity.this, NextActivity.class);
 startActivity(intent);
}
intent.putExtra("key",desired value);