0

在 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();

}
}
4

2 回答 2

0

在您的活动中:

private String question;

@Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putString("question", question);
    super.onSaveInstanceState(outState);
}

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

            if (savedInstanceState != null) {
               question = savedInstanceState.getString(
                "question");
            }

}

编辑:

因为您想永久保存字符串:

示例在共享首选项中保存字符串并在您的应用程序的任何位置再次检索它。

public class PreferencesData {

    public static void saveString(Context context, String key, String value) {
        SharedPreferences sharedPrefs = PreferenceManager
                .getDefaultSharedPreferences(context);
        sharedPrefs.edit().putString(key, intValue).commit();
    }

    public static int getString(Context context, String key) {
        SharedPreferences sharedPrefs = PreferenceManager
                .getDefaultSharedPreferences(context);
        return sharedPrefs.getString(key, "");
    }
}

用法:

PreferencesData.saveString(context, "mynote", "buy more stuff");
// retrieve
String note = PreferencesData.getString(context, "mynote");

使用它在暂停时保存字符串,并在 onCreate 或任何需要信息的地方重新创建它

相同的方法可以很容易地用于其他简单类型。

在你的情况下:

@Override
protected void onPause() {
    PreferencesData.saveString(this, "answer", input.getText().toString());
    super.onPause();
}

在 onCreate() 中:

@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);

    // set to empty string if no value is saved yet
    input.setText(PreferencesData.getString(this, "answer"));

    ....
}
于 2013-09-26T11:54:01.933 回答
0

您应该将您的实例保存在 SharedPreferences 中

例如:您需要保存 String 变量res才能这样做

SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(YourActivityName.this);

Editor edit1 = remembermepref.edit();
                edit1.putString("result_key",youredittext.getText().toString);
                edit1.commit();

要在 onCreate() 中检索结果,请使用以下命令:

SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(YourActivityName.this);
String res1 = pref.getString("result_key");
if(res1.eqauls("correct")
{
youredittext.setText("correct");
}

现在使用您res1想要的方式存储的结果。最重要的是检查您是否使用相同的密钥进行恢复以及将值存储在 SharedPreference

希望这可以帮助..

于 2013-09-26T11:52:01.523 回答