我正在嘗試創建一個測驗測驗。在問題表單中,我製作了3個按鈕,當用戶點擊答案時。它重新啓動相同的佈局和Java類,所以它出現下一個問題。那麼這種方式不太好,因爲一次又一次地調用相同的佈局是很痛苦的。那麼,如何更改代碼,以便在同一個佈局中更改問題20次?我嘗試while
函數,但它不工作,因爲它不等待按下按鈕繼續下一個問題(如果有一個命令等待用戶做出一個動作來繼續while循環,它將工作)。這裏是我的代碼:我怎樣才能讓一個循環等待一個動作先在android中繼續?
package dv.qtest.dvyzual;
import java.util.Random;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class startGame extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.startgame);
final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.correct);
AnyDBAdapter eventsData = new AnyDBAdapter(getBaseContext());
eventsData.open();
myMenu.question = myMenu.question +1;
String qq = getRandomNumber();
Cursor c = eventsData.ExampleSelect(qq);
this.startManagingCursor(c);
eventsData.close(); final String h = c.getString(5); setQuestions(c);
final Button r1 = (Button)findViewById(R.id.b1);
r1.setOnClickListener (new View.OnClickListener() { public void onClick(View v) {
if ("1".equals(h)) {
r1.setBackgroundResource(R.drawable.buttonneocorrect);
myMenu.score = myMenu.score + 1;
mp2.start();
}
else {
r1.setBackgroundResource(R.drawable.buttonneowrong);
}
finish();
if (myMenu.question == 20) {
startActivity(new Intent("dv.qtest.dvyzual.QuizTestActivity.ENDQUIZGAME"));}
else {
startActivity(new Intent("dv.qtest.dvyzual.QuizTestActivity.STARTQUIZGAME"));
}
} }); final Button r2 = (Button)findViewById(R.id.b2);
r2.setOnClickListener (new View.OnClickListener() { public void onClick(View v) {
if ("2".equals(h)) {
r2.setBackgroundResource(R.drawable.buttonneocorrect);
myMenu.score = myMenu.score + 1;
}
else {
r2.setBackgroundResource(R.drawable.buttonneowrong);
}
finish();
if (myMenu.question == 20) {startActivity(new Intent("dv.qtest.dvyzual.QuizTestActivity.ENDQUIZGAME"));}
else {startActivity(new Intent("dv.qtest.dvyzual.QuizTestActivity.STARTQUIZGAME"));} } });
final Button r3 = (Button)findViewById(R.id.b3);
r3.setOnClickListener (new View.OnClickListener() { public void onClick(View v) {
if ("3".equals(h)) {
r3.setBackgroundResource(R.drawable.buttonneocorrect);
myMenu.score = myMenu.score + 1;
mp2.start();
}
else {
r3.setBackgroundResource(R.drawable.buttonneowrong);
}
finish();
if (myMenu.question == 20) {startActivity(new Intent("dv.qtest.dvyzual.QuizTestActivity.ENDQUIZGAME"));}
else {startActivity(new Intent("dv.qtest.dvyzual.QuizTestActivity.STARTQUIZGAME"));} } });
}
private String getRandomNumber(){
Random generator = new Random();
int n = 49;
n = generator.nextInt(n) +1;
String n1 = ""+n;
return n1;
}
private void setQuestions(Cursor c) {
TextView rscore = (TextView)findViewById(R.id.q1); rscore.setText("Score: " + myMenu.score + " Question: "
+ myMenu.question);
TextView r0 = (TextView)findViewById(R.id.textView1); r0.setText(c.getString(1));
Button r1 = (Button)findViewById(R.id.b1); r1.setText(c.getString(2));
Button r2 = (Button)findViewById(R.id.b2); r2.setText(c.getString(3));
Button r3 = (Button)findViewById(R.id.b3); r3.setText(c.getString(4));
} }
你熟悉面向對象編程及其與程序編程的區別? – Joe 2012-01-14 22:57:55
嗯,說實話,因爲我是這門語言的新手,我不太熟悉,也許我在編碼時會犯一些大錯。你看到很多我必須解決的錯誤嗎? – NeoXRated 2012-01-14 23:04:23
對不起,我還沒有花時間看看你的代碼,我只是認識到我在開始使用java編寫代碼(這是我的第一個面向對象的編程語言)之前的思維方式。你的代碼可能沒問題,但它對於理解編碼和解決OOP問題的方法很重要。你不要以任何方式在android中凍結你的代碼(實際上,如果它的話,os會殺死你的應用程序),它需要繼續下去。 – Joe 2012-01-14 23:14:41