0

我仍然在同一个应用程序上,我是编程新手。

所以,EditText 输入不会恢复...我想让应用程序保存 EditText 的输入,当用户再次启动应用程序时,EditText 输入将恢复...

我认为我对 Sharedpreferences 进行了编程,每次单击按钮时,EditText 都会被删除并且不会保存输入或恢复...

我做错了什么?

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    bCalculate = (Button) findViewById(R.id.bCalculate);
    display = (TextView) findViewById(R.id.TvDisplay);

    AdView ad = (AdView) findViewById(R.id.ad);
    ad.loadAd(new AdRequest());

    bCalculate.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

            inputManager.hideSoftInputFromWindow(getCurrentFocus()
                    .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

            int error = 0;
            int anzahlGraden = 0;
            double d1 = 0.0, d2 = 0.0, d3 = 0.0, d4 = 0.0, d5 = 0.0, d6 = 0.0, d7 = 0.0, d8 = 0.0, d9 = 0.0, d10 = 0.0;

            EditText Grade1r = (EditText) findViewById(R.id.Grade1r);
            String Grade1 = Grade1r.getText().toString();

            EditText Grade2r = (EditText) findViewById(R.id.Grade2r);
            String Grade2 = Grade2r.getText().toString();

            EditText Grade3r = (EditText) findViewById(R.id.Grade3r);
            String Grade3 = Grade3r.getText().toString();

            EditText Grade4r = (EditText) findViewById(R.id.Grade4r);
            String Grade4 = Grade4r.getText().toString();

            EditText Grade5r = (EditText) findViewById(R.id.Grade5r);
            String Grade5 = Grade5r.getText().toString();

            EditText Grade6r = (EditText) findViewById(R.id.Grade6r);
            String Grade6 = Grade6r.getText().toString();

            EditText Grade7r = (EditText) findViewById(R.id.Grade7r);
            String Grade7 = Grade7r.getText().toString();

            EditText Grade8r = (EditText) findViewById(R.id.Grade8r);
            String Grade8 = Grade8r.getText().toString();

            EditText Grade9r = (EditText) findViewById(R.id.Grade9r);
            String Grade9 = Grade9r.getText().toString();

            EditText Grade10r = (EditText) findViewById(R.id.Grade10r);
            String Grade10 = Grade10r.getText().toString();

            if (Grade1.equals("")) {
                error++;
            } else {
                d1 = Double.parseDouble(Grade1);
                anzahlGraden++;
            }

            if (Grade2.equals("")) {
                error++;
            } else {
                d2 = Double.parseDouble(Grade2);
                anzahlGraden++;
            }

            if (Grade3.equals("")) {
                error++;
            } else {
                d3 = Double.parseDouble(Grade3);
                anzahlGraden++;
            }

            if (Grade4.equals("")) {
                error++;
            } else {
                d4 = Double.parseDouble(Grade4);
                anzahlGraden++;
            }

            if (Grade5.equals("")) {
                error++;
            } else {
                d5 = Double.parseDouble(Grade5);
                anzahlGraden++;
            }

            if (Grade6.equals("")) {
                error++;
            } else {
                d6 = Double.parseDouble(Grade6);
                anzahlGraden++;
            }

            if (Grade7.equals("")) {
                error++;
            } else {
                d7 = Double.parseDouble(Grade7);
                anzahlGraden++;
            }

            if (Grade8.equals("")) {
                error++;
            } else {
                d8 = Double.parseDouble(Grade8);
                anzahlGraden++;
            }

            if (Grade9.equals("")) {
                error++;
            } else {
                d9 = Double.parseDouble(Grade9);
                anzahlGraden++;
            }

            if (Grade10.equals("")) {
                error++;
            } else {
                d10 = Double.parseDouble(Grade10);
                anzahlGraden++;
            }

            if (error > 8) {
                display.setText("Please enter more then 2 grades.");
            } else {

                double gesamt = d1 + d2 + d3 + d4 + d5 + d6 + d7 + d8 + d9
                        + d10;

                double resultat = gesamt / anzahlGraden;

                display.setText("Your average is " + resultat);

            }

             mPrefs = getSharedPreferences(Grade1, 0);

             String rememberedText1 = mPrefs.getString("grade1r", "");
             grade1r = (EditText) findViewById(R.id.Grade1r);
             grade1r.setText(rememberedText1);
        }

    });

}
 protected void onPause() {
     super.onPause();

     SharedPreferences.Editor ed = mPrefs.edit();
     ed.putString("grade1r", grade1r.getText().toString());
     ed.commit();
 }

}

4

2 回答 2

1

//您仅在 onPause() 方法中将数据保存到共享首选项。

顺便说一句,当您单击按钮时,您正在阅读“grade1r”,它将为空,因此默认值为“”即将到来

String rememberedText1 = mPrefs.getString("grade1r", "");

如果您想检查默认打印,请这样做

String rememberedText1 = mPrefs.getString("grade1r", "myDefault value");
于 2012-05-23T12:55:44.640 回答
0

当您单击一个按钮时,您会从 SharedPreferences 中恢复一个值。您的默认值是一个空字符串。但是,您在暂停应用程序时会保存一个值。尝试在文本框中输入一些文本并关闭您的应用程序,然后启动您的应用程序并单击该按钮,文本应该会恢复。

于 2012-05-23T13:03:10.550 回答