在 LogoQuiz 游戏中,当我们输入正确答案时,会显示答案并取消活动。每当我们返回该屏幕时,都会显示相同的内容。这是怎么做的?谁能提供一个例子?
public class Activity3 extends Activity implements OnClickListener {
private Button Hint1;
private Button Hint2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity3);
Hint1 = (Button) findViewById(R.id.Hint1);
Hint2 = (Button) findViewById(R.id.Hint2);
Hint1.setOnClickListener(this);
Hint2.setOnClickListener(this);
Hint2.setEnabled(false);
final EditText input = (EditText) findViewById(R.id.answer);
final TextView display = (TextView) findViewById(R.id.response);
input.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
String check = input.getText().toString().replace(" ","");
if(check.equalsIgnoreCase("good")){
display.setText("correct");
input.setEnabled(false);
} else{
display.setText("Wrong");
}
return true;
}
return false;}
});
}
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.Hint1:
showOneButtonDialog();
break;
case R.id.Hint2:
showTwoButtonDialog();
break;
}}
private void showOneButtonDialog()
{
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
dialogBuilder.setTitle(" Hint 1 ");
Hint2.setEnabled(true);
dialogBuilder.setMessage(" This is your Hint1");
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
}
private void showTwoButtonDialog()
{
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
dialogBuilder.setTitle(" Hint 2 ");
dialogBuilder.setMessage("This is your Hint2 ");
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
}
}