0

我是这个android领域的初学者,目前正在开发一个应用程序来跟踪我在edittext中使用计数器和主题名称时使用的周期数,并且计数器的值随着按钮的增加而不断增加每次按下 bunked ,其更改将分别在文本字段中生效按钮应用程序退出,现在当用户再次进入应用程序时,以前输入的所有数据都消失了......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);
    }
}
4

1 回答 1

0

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

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

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

于 2013-07-10T10:25:14.493 回答