保存和还原实例

时间:2013-09-26 11:44:11

标签: android

在我们输入正确答案的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();

}
}

2 个答案:

答案 0 :(得分: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中存储的结果。最重要的是要检查是否使用了相同的密钥进行恢复以及ws将值存储在SharedPreference中

希望这会有所帮助..

答案 1 :(得分: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"));

    ....
}