我这里有这个代码,它基本上产生了一个数字猜谜游戏。一旦三次尝试过期,它将带您进入Game Over屏幕,并显示您的分数。在游戏结束屏幕上,您可以单击“重试”按钮,但它不会再次打开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();
}
}
Game Over Layout:
<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>
答案 0 :(得分:0)
正如评论中提到的那样,你必须在你的布局中添加方法tryAgainButton
作为onClickListener,否则如果你不以编程方式添加onclick方法就没有任何反应:
<强>机器人:的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" />