1

我有这段代码,我想在更改或开始新活动后从第一个输入中保留我的编辑框值。

这在这段代码中发生了什么:

editbox1 = 1 > start new activity > back to recent activity > editbox1 = null

我需要发生这种情况:

editbox1 = 1 > start new activity > back to recent activity > editbox1 = 1

代码

package org.example.touch;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.*;
import android.widget.EditText;


public class SettingsClass extends Activity {


    private EditText Alpha;
    private EditText Beta;
    private EditText Charlie;
    private EditText Delta;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);

        Alpha = (EditText) findViewById(R.id.editText1);
        Beta = (EditText) findViewById(R.id.editText2);
        Charlie = (EditText) findViewById(R.id.editText3);
        Delta = (EditText) findViewById(R.id.editText4);    

    }

    public void buttonSBHandler (View view){        

        String Aint = Alpha.getText().toString();
        String Bint = Beta.getText().toString();
        String Cint = Charlie.getText().toString();
        String Dint = Delta.getText().toString();

        Intent startNewActivityOpen = new Intent(SettingsClass.this, GameUi.class);

        startNewActivityOpen.putExtra("Aint", Aint);
        startNewActivityOpen.putExtra("Bint", Bint);
        startNewActivityOpen.putExtra("Cint", Cint);
        startNewActivityOpen.putExtra("Dint", Dint);
        startActivityForResult(startNewActivityOpen, 0);
        //startActivity(new Intent(view.getContext(), GameUi.class));
        }
}
4

1 回答 1

2

1)One thing is that you can go with shared preference store your value in shared preference and on oncreate() method first check whether shared preference is null or not if it is not null than get the value from shared preference.

or

2)Just make those data of edit text as static like-

static String Aint = Alpha.getText().toString();
      static   String Bint = Beta.getText().toString();
        static String Cint = Charlie.getText().toString();
       static String Dint = Delta.getText().toString();

so whenever you will come back to the activity so the previous data of the edit text will be shown there. hope these thing will work for you perfectly. thanks

于 2012-10-27T07:12:41.683 回答