0

如何将文本保存为文本编辑,以便在离开活动并重新输入后,它会出现在相同的编辑文本中?

    switchbutton.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            Switch sw = (Switch) v;

            if (sw.isChecked()) {
                SharedPreferences settings = getPreferences(0);
                SharedPreferences.Editor editor1 = settings.edit();
                editor1.putString("nombre", ed1.getText().toString());
                SharedPreferences.Editor editor2 = settings.edit();
                editor2.putString("apellido", ed2.getText().toString());
                SharedPreferences.Editor editor3 = settings.edit();
                editor3.putString("ciudad", ed3.getText().toString());
                SharedPreferences.Editor editor4 = settings.edit();
                editor4.putString("calle", ed4.getText().toString());
                SharedPreferences.Editor editor5 = settings.edit();
                editor5.putString("numero", ed5.getText().toString());
                editor1.commit();
                editor2.commit();
                editor3.commit();
                editor4.commit();
                editor5.commit();   
            } else {

                Toast.makeText(OtraActivity.this, "Datos no guardados",
                        Toast.LENGTH_LONG).show();
            }
        }});
4

3 回答 3

2

正如 cloudymusic 建议的那样,使用 SharedPreferences,您的活动onPause()存储数据,然后onResume()再次加载,即使应用程序关闭,您的 EditText 数据也会保持不变

于 2013-01-09T17:33:47.087 回答
1

onCreateonStart或任何您加载视图的地方,您可以从首选项加载数据并将其插入到您的视图中:

@Override
protected void onCreate(Bundle savedInstanceState) {
    SharedPreferences settings = getPreferences(0);
    ed1.setText(settings.getString("nombre", ""));
    ed2.setText(settings.getString("apellido", ""));
    ...
}

onPause中,您可以像单击按钮时一样保存它。

于 2013-01-09T17:34:50.940 回答
1

使用以下 onCreate

SharedPreferences 设置 = getPreferences(0); ed1.setText(settings.getString("名词",""));

对其他edittext做同样的事情..

于 2013-01-09T17:36:09.270 回答