我正在尝试制作记忆式游戏。一切都很好,但如果它不匹配则不显示第二张牌。这是因为它在switch语句完成之后才执行setText(),但是我让它在退出交换机之前将卡切换回比较。我以为我可以使用setText()并在比较之前等待几秒钟但事实并非如此。有没有更好的方法可以做到这一点或强制setText()立即发生而不是退出交换机的方式?
public class Game extends Activity implements OnClickListener
{
private Button buttonA1;
private Button buttonA2;
private Button buttonA3;
private Button buttonA4;
private Button buttonB1;
private Button buttonB2;
private Button buttonB3;
private Button buttonB4;
private Button buttonC1;
private Button buttonC2;
private Button buttonC3;
private Button buttonC4;
private Button choice1;
private Button choice2;
private Button reset;
private Model model;
private int cardNum=1;
private int turn=1;
private TextView turnDisplay;
private Context context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_concentration);
start();
context = getApplicationContext();
model = new Model();
buttonA1= (Button) findViewById(R.id.button1);
buttonA1.setOnClickListener(this);
buttonA2= (Button) findViewById(R.id.button2);
buttonA2.setOnClickListener(this);
buttonA3= (Button) findViewById(R.id.button3);
buttonA3.setOnClickListener(this);
buttonA4= (Button) findViewById(R.id.button10);
buttonA4.setOnClickListener(this);
buttonB1= (Button) findViewById(R.id.button4);
buttonB1.setOnClickListener(this);
buttonB2= (Button) findViewById(R.id.button5);
buttonB2.setOnClickListener(this);
buttonB3= (Button) findViewById(R.id.button6);
buttonB3.setOnClickListener(this);
buttonB4= (Button) findViewById(R.id.button11);
buttonB4.setOnClickListener(this);
buttonC1= (Button) findViewById(R.id.button7);
buttonC1.setOnClickListener(this);
buttonC2= (Button) findViewById(R.id.button8);
buttonC2.setOnClickListener(this);
buttonC3= (Button) findViewById(R.id.button9);
buttonC3.setOnClickListener(this);
buttonC4= (Button) findViewById(R.id.button12);
buttonC4.setOnClickListener(this);
reset= (Button) findViewById(R.id.button13);
reset.setOnClickListener(this);
turnDisplay= (TextView) findViewById(R.id.turnView);
}
@Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.button1:
buttonA1.setText(model.entityList.get(0));
if(cardNum ==1)
{
choice1=buttonA1;
cardNum++;
}
else
{
choice2=buttonA1;
compCard(choice1, choice2);
turn++;
}
break;
case R.id.button2:
buttonA2.setText(model.entityList.get(1));
if(cardNum ==1)
{
choice1=buttonA2;
cardNum++;
}
else
{
choice2=buttonA2;
compCard(choice1, choice2);
turn++;
}
break;
case R.id.button3:
buttonA3.setText(model.entityList.get(2));
if(cardNum ==1)
{
choice1=buttonA3;
cardNum++;
}
else
{
choice2=buttonA3;
compCard(choice1, choice2);
turn++;
}
break;
case R.id.button10:
buttonA4.setText(model.entityList.get(9));
if(cardNum ==1)
{
choice1=buttonA4;
cardNum++;
}
else
{
choice2=buttonA4;
compCard(choice1, choice2);
turn++;
}
break;
case R.id.button4:
buttonB1.setText(model.entityList.get(3));
if(cardNum ==1)
{
choice1=buttonB1;
cardNum++;
}
else
{
choice2=buttonB1;
compCard(choice1, choice2);
turn++;
}
break;
case R.id.button5:
buttonB2.setText(model.entityList.get(4));
if(cardNum ==1)
{
choice1=buttonB2;
cardNum++;
}
else
{
choice2=buttonB2;
compCard(choice1, choice2);
turn++;
}
break;
case R.id.button6:
buttonB3.setText(model.entityList.get(5));
if(cardNum ==1)
{
choice1=buttonB3;
cardNum++;
}
else
{
choice2=buttonB3;
compCard(choice1, choice2);
turn++;
}
break;
case R.id.button11:
buttonB4.setText(model.entityList.get(10));
if(cardNum ==1)
{
choice1=buttonB4;
cardNum++;
}
else
{
choice2=buttonB4;
compCard(choice1, choice2);
turn++;
}
break;
case R.id.button7:
buttonC1.setText(model.entityList.get(6));
if(cardNum ==1)
{
choice1=buttonC1;
cardNum++;
}
else
{
choice2=buttonC1;
compCard(choice1, choice2);
turn++;
}
break;
case R.id.button8:
buttonC2.setText(model.entityList.get(7));
if(cardNum ==1)
{
choice1=buttonC2;
cardNum++;
}
else
{
choice2=buttonC2;
compCard(choice1, choice2);
turn++;
}
break;
case R.id.button9:
buttonC3.setText(model.entityList.get(8));
if(cardNum ==1)
{
choice1=buttonC3;
cardNum++;
}
else
{
choice2=buttonC3;
compCard(choice1, choice2);
turn++;
}
break;
case R.id.button12:
buttonC4.setText(model.entityList.get(11));
if(cardNum ==1)
{
choice1=buttonC4;
cardNum++;
}
else
{
choice2=buttonC4;
compCard(choice1, choice2);
turn++;
}
break;
//resets the game
case R.id.button13:
finish();
Music.stop(this);
Intent x=new Intent(this, Game.class);
startActivity(x);
break;
}
}
private void compCard(Button choice1, Button choice2) {
//gets if the 2 cards match
turnDisplay.setText(turn+" ");
if(model.answerMap.get(choice1.getText())==choice2.getText()||
model.answerMap.get(choice2.getText())==choice1.getText())
{
int duration = Toast.LENGTH_SHORT;
// choice1.setVisibility(Button.GONE);
// choice2.setVisibility(Button.GONE);
choice1.setBackgroundColor(color.black);
choice2.setBackgroundColor(color.white);
Toast toast = Toast.makeText(context, ""+choice1.getText() + " and " + choice2.getText() + " are a match",duration);
// Toast toast2 = Toast.makeText(context, "Boom Headshot!", duration);
toast.show();
cardNum=1;
}
else
{
int duration=Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, ""+choice1.getText() + " and " + choice2.getText() + " are not a match ",duration);
toast.show();
choice1.setText("Match");
choice2.setText("Match");
cardNum=1;
}
}
答案 0 :(得分:0)
为什么不记得选项(字符串)的文本并比较而不是比较按钮?那么你的比较函数只是if (string1.equals(string2))
。