EditText / TextView结果上的sharedPreference或onSaveInstanceState

时间:2015-04-14 23:42:26

标签: android android-activity sharedpreferences savestate

我在下面有一个小测试项目。我想要做的就是保存输入的EditText数字和TextView结果(thing1,thing2,result)。什么是最好的? onSaveInstanceState,sharedPreference,还是像SQLite一样不同的东西?

我令人沮丧地尝试了前两个(可能是令人尴尬的太久),但无法弄明白。有人可以通过将其添加到下面的代码来帮助吗?

public class MainActivity extends ActionBarActivity {



EditText thing1;
EditText thing2;
TextView result;

double n1=0;
double n2=0;
double total=0;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    final Button divideButton = (Button) findViewById(R.id.divideButton);
    divideButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            thing1 = (EditText) findViewById(R.id.thing1);
            if (TextUtils.isEmpty(thing1.getText().toString())) {
                n1 = 0;}
            else {
                 n1= Double.parseDouble(thing1.getText().toString());
            }

            thing2 = (EditText) findViewById(R.id.thing2);
            if (TextUtils.isEmpty(thing2.getText().toString())) {
                n2 = 0;}
            else {
                n2 = Double.parseDouble(thing2.getText().toString());
            }

            if (n2 !=0){

             total = (n1 / n2);}


            final double total =  ((double)n1/(double)n2);

            final TextView result= (TextView) findViewById(R.id.result);

            String foo = String.format("%.2f", total);
            result.setText(foo);


        }

    });


    final Button addButton = (Button) findViewById(R.id.addButton);
    addButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            thing1 = (EditText) findViewById(R.id.thing1);
            if (TextUtils.isEmpty(thing1.getText().toString())) {
                n1 = 0;}
            else {
                n1= Double.parseDouble(thing1.getText().toString());
            }

            thing2 = (EditText) findViewById(R.id.thing2);
            if (TextUtils.isEmpty(thing2.getText().toString())) {
                n2 = 0;}
            else {
                n2 = Double.parseDouble(thing2.getText().toString());
            }


            final double total =  (n1+n2);

            final TextView result= (TextView) findViewById(R.id.result);

            String foo = String.format("%.2f", total);
            result.setText(foo);


        }

    });


    final Button subtractButton = (Button) findViewById(R.id.subtractButton);
    subtractButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            thing1 = (EditText) findViewById(R.id.thing1);
            if (TextUtils.isEmpty(thing1.getText().toString())) {
                n1 = 0;}
            else {
                n1= Double.parseDouble(thing1.getText().toString());
            }

            thing2 = (EditText) findViewById(R.id.thing2);
            if (TextUtils.isEmpty(thing2.getText().toString())) {
                n2 = 0;}
            else {
                n2 = Double.parseDouble(thing2.getText().toString());
            }




            final double total =  (n1-n2);

            final TextView result= (TextView) findViewById(R.id.result);

            String foo = String.format("%.2f", total);
            result.setText(foo);


        }

    });


    final Button multiplyButton = (Button) findViewById(R.id.multiplyButton);
    multiplyButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            thing1 = (EditText) findViewById(R.id.thing1);
            if (TextUtils.isEmpty(thing1.getText().toString())) {
                n1 = 0;}
            else {
                n1= Double.parseDouble(thing1.getText().toString());
            }

            thing2 = (EditText) findViewById(R.id.thing2);
            if (TextUtils.isEmpty(thing2.getText().toString())) {
                n2 = 0;}
            else {
                n2 = Double.parseDouble(thing2.getText().toString());
            }


            final double total =  (n1*n2);

            final TextView result= (TextView) findViewById(R.id.result);

            String foo = String.format("%.2f", total);
            result.setText(foo);



        }

    });
}

1 个答案:

答案 0 :(得分:0)

如果您稍后会像打开应用程序时那样使用这些值,那么您就是 可以使用SharedPreferences。根据您上面的代码,您可以添加以下代码 将EditText数据保存到SharedPreferences并稍后恢复。

保存EditText值:

SharedPreferences sharedPreferences = getApplication().getSharedPreferences("ProjectName", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("thing1", thing1.getText.toString());    
editor.commit();

要从SharedPreferences获取值,您可以在onCreate函数中执行此操作:

SharedPreferences sharedPreferences = getApplication().getSharedPreferences("ProjectName", MODE_PRIVATE);
String sValue = sharedPreferences.getString("thing1", "default");
thing1.setText( sValue );
相关问题