android应用程序开发 - 保存和检索editText和textView的值

时间:2013-07-10 08:29:08

标签: java android xml eclipse

我是这个android领域的初学者,我目前正在寻求开发一个应用程序来跟踪我使用计数器和edittext中的主题名称进行压缩的周期数,并且计数器的值继续增加因为每次按下bunked按钮,它的变化将分别在文本字段中进行 我的应用程序只有一个活动,当用户完成在edittext中输入数据并且计数器递增时,在textview中执行值,当用户按下后退按钮时,应用程序退出,现在当用户再次进入应用程序时现在所有以前输入的数据都消失了...... edittext或textview中没有数据...

public class MainActivity extends Activity {

    public static final String sub1="sub";
    public static final String count1="Count";
    String sub[]=new String[10];
    String count[]=new String[10];
    int counter[]=new int[10];
    Button button[]=new Button[10];
    EditText et[]=new EditText[10];
    TextView tv[]=new TextView[12];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 
        button[0] = (Button) findViewById(R.id.button1);
        button[1] = (Button) findViewById(R.id.button2);
        button[2] = (Button) findViewById(R.id.button3);
        button[3] = (Button) findViewById(R.id.button4);
        button[4] = (Button) findViewById(R.id.button5);
        button[5] = (Button) findViewById(R.id.button6);
        button[6] = (Button) findViewById(R.id.button7);
        button[7] = (Button) findViewById(R.id.button8);
        button[8] = (Button) findViewById(R.id.button9);

        tv[0] = (TextView) findViewById(R.id.textView2);
        tv[1] = (TextView) findViewById(R.id.textView3);
        tv[2] = (TextView) findViewById(R.id.textView4);
        tv[3] = (TextView) findViewById(R.id.textView5);
        tv[4] = (TextView) findViewById(R.id.textView6);
        tv[5] = (TextView) findViewById(R.id.textView7);
        tv[6] = (TextView) findViewById(R.id.textView8);
        tv[7] = (TextView) findViewById(R.id.textView9);
        tv[8] = (TextView) findViewById(R.id.textView10);
        tv[9] = (TextView) findViewById(R.id.textView1);

        et[0] = (EditText) findViewById(R.id.editText1);
        et[1] = (EditText) findViewById(R.id.editText2);
        et[2] = (EditText) findViewById(R.id.editText3);
        et[3] = (EditText) findViewById(R.id.editText4);
        et[4] = (EditText) findViewById(R.id.editText5);
        et[5] = (EditText) findViewById(R.id.editText6);
        et[6] = (EditText) findViewById(R.id.editText7);
        et[7] = (EditText) findViewById(R.id.editText8);
        et[8] = (EditText) findViewById(R.id.editText9);

        for(int l=0;l<9;l++) {
            sub[l]=et[l].getText().toString();
            et[l].setText(sub[l]);
        }

        for (int j = 0; j < 9; j++) {
            button[j].setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                for(int k=0;k<9;k++) {
                    if(button[k]==arg0.findViewById(R.id.button1)) {
                        counter[k]++;
                        tv[k].setText(""+counter[k]);
                    }           

                    if(button[k]==arg0.findViewById(R.id.button2)) {
                        counter[k]++;
                        tv[k].setText(""+counter[k]);
                    }

                    if(button[k]==arg0.findViewById(R.id.button3)) {
                        counter[k]++;
                        tv[k].setText(""+counter[k]);
                    }                   

                    if(button[k]==arg0.findViewById(R.id.button4)) {
                        counter[k]++;
                        tv[k].setText(""+counter[k]);
                    }                   

                    if(button[k]==arg0.findViewById(R.id.button5)) {
                        counter[k]++;
                        tv[k].setText(""+counter[k]);
                    }                   

                    if(button[k]==arg0.findViewById(R.id.button6)) {
                        counter[k]++;
                        tv[k].setText(""+counter[k]);
                    }                   

                    if(button[k]==arg0.findViewById(R.id.button7)) {
                        counter[k]++;
                        tv[k].setText(""+counter[k]);
                    }                   

                    if(button[k]==arg0.findViewById(R.id.button8)) {
                        counter[k]++;
                        tv[k].setText(""+counter[k]);
                    }                   

                    if(button[k]==arg0.findViewById(R.id.button9)) {
                        counter[k]++;
                        tv[k].setText(""+counter[k]);
                    }                               
               }
          }
    });
  }
}

    @Override
    protected void onSaveInstanceState(Bundle savedInstanceState) {
        savedInstanceState.putString(sub1,sub[0]);
        savedInstanceState.putInt(count1, counter[0]);
        super.onSaveInstanceState(savedInstanceState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        sub[0]=savedInstanceState.getString(sub1);
        counter[0]=savedInstanceState.getInt(count1);
    }
}

1 个答案:

答案 0 :(得分:0)

它的发生是因为您要保存数组中的所有数据,这些数据会在应用程序退出时进行清理。有几种方法可以做到这一点。您可以使用SharedPreferences保存数据或将值写入文件。 对于例如您可以按如下方式使用SharedPreferences:

public static final String MY_PREF = "MyPreference";
SharedPreferences.Editor editorPref;
public static String keyName = "MyKey";
SharedPreferences myPref;

然后,您可以创建共享首选项并向其添加值。希望这有助于