当if语句成立时打开一个行为

时间:2017-05-20 13:43:31

标签: 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.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;
       }
    }
}

我要打开的活动会向玩家显示他的分数。活动等于所有游戏模式,将对应用程序进行一些测试,了解应该进行的路径。测试像这样: &#34;

if (gameStatus == 12) {
            gameScore = gameScore*55;
            TextView scoreText = (TextView) findViewById(R.id.textView8);
            scoreText.setText(gameScore);
        }
        else if (gameStatus == 15){

&#34; 有4种游戏模式:这是6x4游戏,我们可以找到24张卡片(12张图片)。

1 个答案:

答案 0 :(得分:0)

else if (gameStatus == 15){
 Intent intent = new Intent(Game6x4Activity.this, NextActivity.class);
 startActivity(intent);
}

我想,你这是要求的。您可以使用

将值传递给另一个活动
intent.putExtra("key",desired value);