我是Android开发的初学者。我正在创建一个像4个图片1个单词的游戏。我的代码还没有完成。我的问题是,我不能将一个OnClickListener放在IF语句中。
我需要随机化问题所以我把随机生成器然后使用if语句来呈现随机数给出的问题。我使用for循环,以便在所有游戏都播放之前游戏不会结束。这是我的
中的代码MainActivity :
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
Button btnStart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStart = (Button) findViewById(R.id.btnStart);
btnStart.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btnStart:
startActivity(new Intent(this, game.class));
break;
}
}
}
游戏:
public class game extends AppCompatActivity{
EditText etAnswer;
Button btnSubmit;
ImageView image1;
ImageView image2;
ImageView image3;
ImageView image4;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
etAnswer = (EditText) findViewById(R.id.etAnswer);
image1 = (ImageView) findViewById(R.id.image1);
image2 = (ImageView) findViewById(R.id.image2);
image3 = (ImageView) findViewById(R.id.image3);
image4 = (ImageView) findViewById(R.id.image4);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
ArrayList<Integer> imageList = new ArrayList<>();
imageList.add(R.drawable.andresbonifacio1);
for (int i = 0; i >= imageList.size(); i++) {
Random generator = new Random();
int number = generator.nextInt(5) + 1;
if (number == 1) {
image1.setImageResource(R.drawable.andresbonifacio1);
new GameData().questionOne();
} else if (number == 2) {
image1.setImageResource(R.drawable.andresbonifacio1);
new GameData().questionTwo();
} else if (number == 3) {
image1.setImageResource(R.drawable.andresbonifacio1);
new GameData().questionThree();
} else if (number == 4) {
image1.setImageResource(R.drawable.andresbonifacio1);
new GameData().questionFour();
} else {
image1.setImageResource(R.drawable.andresbonifacio1);
new GameData().questionFive();
}
}
}
}
这是我在 GameData
中的代码public class GameData extends game{
public void questionOne() {
image1.setImageResource(R.drawable.andresbonifacio1);
String answer = etAnswer.getText().toString();
if (answer.equals("Jose Rizal") || answer.equals("jose rizal") || answer.equals("Rizal") || answer.equals("rizal")) {
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
dlgAlert.setMessage("The famous Rizal monument in Luneta was not the work of a Filipino but a Swiss sculptor named Richard Kissling?" +
"\n" +
"\n" +
"Source: http://www.joserizal.ph/ta01.html");
dlgAlert.setTitle("Did you know that ...");
dlgAlert.setPositiveButton("Next",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//statements
new game();
}
});
dlgAlert.setCancelable(true);
dlgAlert.create().show();
new game();
} else {
Context context = getApplicationContext();
CharSequence text = "Wrong! Try Again.";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
public void questionTwo() {
String answer = etAnswer.getText().toString();
if (answer.equals("Antonio Luna") || answer.equals("antonio luna") || answer.equals("Heneral Luna") || answer.equals("heneral luna")) {
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
dlgAlert.setMessage("He is the younger brother of the famous Filipino painter Juan Luna who is known for his masterpiece Spoliarium." +
"\n" +
"\n" +
"Source: http://nobert-bermosa.blogspot.com/2011/05/70-interesting-facts-about-general.html");
dlgAlert.setTitle("Did you know that ...");
dlgAlert.setPositiveButton("Next",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
new game();
}
});
dlgAlert.setCancelable(true);
dlgAlert.create().show();
new game();
} else {
Context context = getApplicationContext();
CharSequence text = "Wrong! Try Again.";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
public void questionThree() {
String answer = etAnswer.getText().toString();
if (answer.equals("Apolinario Mabini") || answer.equals("Mabini") || answer.equals("apolinario mabini") || answer.equals("mabini")) {
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
dlgAlert.setMessage("His complete name is Apolinario Marana Mabini and was nicknamed Poly." +
"\n" +
"\n" +
"Source: http://nobert-bermosa.blogspot.com/2011/04/50-interesting-facts-about-apolinario.html");
dlgAlert.setTitle("Did you know that ...");
dlgAlert.setPositiveButton("Next",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
new game();
}
});
dlgAlert.setCancelable(true);
dlgAlert.create().show();
new game();
} else {
Context context = getApplicationContext();
CharSequence text = "Wrong! Try Again.";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
public void questionFour() {
String answer = etAnswer.getText().toString();
if (answer.equals("Melchora Aquino") || answer.equals("melchora aquino") || answer.equals("Tandang Sora") || answer.equals("tandang sora")) {
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
dlgAlert.setMessage("She had a talent in singing and was very well known for singing at the mass. She was also often chosen for the role of “Reyna Elena” during the Santacruzan. " +
"\n" +
"\n" +
"Source: biography.yourdictionary.com");
dlgAlert.setTitle("Did you know that ...");
dlgAlert.setPositiveButton("Next",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
new game();
}
});
dlgAlert.setCancelable(true);
dlgAlert.create().show();
new game();
} else {
Context context = getApplicationContext();
CharSequence text = "Wrong! Try Again.";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
public void questionFive() {
String answer = etAnswer.getText().toString();
if(answer.equals("Andres Bonifacio") || answer.equals("andres bonifacio")){
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
dlgAlert.setMessage("It was a secret society after all. The final test to be a Katipunero was a blood compact (sandugo) " +
"reminiscent of the first one Filipinos shared with the Spaniards early in history. It was the Katipunan’s way of " +
"capturing the narrative from the “traitors” and making brotherhood their own." +
"But the twist to the Katipunan’s sandugo was that they wrote their oath in their own blood. After this, " +
"they chose a symbolic name for themselves. Quite interesting was the Supremo’s choice of name, and quite " +
"telling too, for a leader of a hopeful nation: MAYPAG-ASA." +
"\n" +
"\n" +
"http://www.rappler.com/nation/44920-andres-bonifacio-myths-trivia-execution");
dlgAlert.setTitle("Did you know that ...");
dlgAlert.setPositiveButton("Next",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
new game();
}
});
dlgAlert.setCancelable(true);
dlgAlert.create().show();
new game();
}else{
Context context = getApplicationContext();
CharSequence text = "Wrong! Try Again.";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}}
答案 0 :(得分:3)
为什么要将onClickListener
放入if-else
阻止?
您可以做的是将if-else
阻止放在onClickListener
内。像这样的东西,
youtButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if(..)
...
else ....
}
});
答案 1 :(得分:0)
将OnClickListener按钮设置为匿名类。试试这段代码:
public class MainActivity extends AppCompatActivity{
Button btnStart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStart = (Button) findViewById(R.id.btnStart);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//insert your condition here
startActivity(new Intent(this, game.class));
}
});
}
}
希望这对你有所帮助。
此外,U可以简化代码:
findViewById(R.id.btnStart).setOnClickListener(new View.OnClickListener() { ... })
更新: U可以在启动活动之后实现调用指定的方法,如下所示:
Intent intent = new Intent(context, game.class);
intent.putExtra("startFuncOne", true);
startActivity(intent);
然后在你的游戏活动中:
if (getIntent().getBooleanExtra("startFuncOne", false)) {
functionOne();
}