如何将保存的文本转换为编辑文本

时间:2013-01-09 17:25:01

标签: android

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

    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();
            }
        }});

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

onCreateonStart或您加载视图的任何位置,您都可以从prefs加载数据并将其插入到您的视图中:

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

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

答案 2 :(得分:1)

使用以下onCreate

SharedPreferences settings = getPreferences(0);                  ed1.setText(settings.getString( “农布雷”, “”));

对其他edittext执行相同的操作..