在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();
}
}
你的問題不是很清楚。你可以擴大,顯示一些代碼... – Szymon
我已經添加了代碼。現在,當用戶在Edittext中輸入good時,將顯示「正確」,並且我希望它永遠如此 – user2819290