0

我这里有这段代码,它基本上产生了一个猜数字游戏。三次尝试过期后,它会将您带到 Game Over 屏幕,并应显示您的分数。在 Game Over 屏幕上,您可以单击 Try Again 按钮,但它不符合再次打开 MainActivity 的意图。任何指针?

主要活动:

public class MainActivity extends Activity {
int n = 0;
Random random = new Random();
static int score = 0;
int m = random.nextInt(9 - 1) + 1;
int tries = 3;

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

public static String scoreFinal = "scoreFinal";

public void arg(){
    if (tries < 1){
        Intent intent = new Intent(this, GameOver.class);
        TextView text = (TextView) findViewById(R.id.scoreFinalText);
        intent.putExtra(scoreFinal, score);
        startActivity(intent);
        finish();
    } else {
    if (n == m) {
        TextView text = (TextView) findViewById(R.id.status);
        text.setText("You are correct! I've come up with a new number, can you guess it?.");
        score += 1;
        tries = 3;
        m = random.nextInt(9 - 1) + 1;
    } else if (n < m){
        tries -= 1;
        TextView text = (TextView) findViewById(R.id.status);
        text.setText("You are incorrect. This number is lower then mine.");
    } else if (n > m){
        tries -= 1;
        TextView text = (TextView) findViewById(R.id.status);
        text.setText("You are incorrect. This number is higher then mine.");
    }
    TextView text = (TextView) findViewById(R.id.scoreNum);
    String i = Integer.toString(score);
    text.setText(i);

    TextView text2 = (TextView) findViewById(R.id.triesNum);
    String j = Integer.toString(tries);
    text2.setText(j);
    }
}
public void send1(View view) {
    n = 1;
    arg();

}
public void send2(View view) {
    n = 2;
    arg();
}
public void send3(View view) {
    n = 3;
    arg();

}
public void send4(View view) {
    n = 4;
    arg();

}
public void send5(View view) {
    n = 5;
    arg();

}
public void send6(View view) {
    n = 6;
    arg();

}
public void send7(View view) {
    n = 7;
    arg();

}
public void send8(View view) {
    n = 8;
    arg();

}
public void send9(View view) {
    n = 1;
    arg();

}

}

游戏结束画面:

public class GameOver extends Activity {

@SuppressLint("NewApi")

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

    // Get the message from the intent
    Intent intent = getIntent();
    String scoreFinal = intent.getStringExtra(MainActivity.scoreFinal);

    // Create the text view
    TextView text = (TextView) findViewById(R.id.scoreFinalText);
    text.setText(scoreFinal);
}


public void tryAgainButton(View view) {
    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);
    finish();

}

}

游戏结束布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/gameOver"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="42dp"
    android:text="Game Over!"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/scoreFinalText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/scoreInfo"
    android:layout_centerHorizontal="true"
    android:text="0"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/scoreInfo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/gameOver"
    android:layout_below="@+id/gameOver"
    android:layout_marginTop="17dp"
    android:text="Your score was:"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<Button
    android:id="@+id/tryAgainButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/scoreInfo"
    android:layout_below="@+id/scoreFinalText"
    android:layout_marginTop="92dp"
    android:text="Try Again?" />

</RelativeLayout>
4

1 回答 1

0

正如评论中提到的 tgrll ,您必须tryAgainButton在布局中将该方法添加为 onClickListener ,否则如果您不以编程方式添加 onclick 方法,则不会发生任何事情:

android:onClick="tryAgainButton"

<Button
    android:id="@+id/tryAgainButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/scoreInfo"
    android:layout_below="@+id/scoreFinalText"
    android:layout_marginTop="92dp"
    android:text="Try Again?" 
    android:onClick="tryAgainButton" />
于 2013-08-22T19:44:23.953 回答