打后退按钮时保存数据的EditText

打后退按钮时保存数据的EditText

本文介绍了打后退按钮时保存数据的EditText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以练习1我点击一个按钮,带我去的活动2.在活动2我输入一些数据到一个EditText。当我打它带我到活动1这是正确的手机上的后退按钮,但如果我打的活动1按钮再次,我进入的EditText任何文本已经一去不复返了。我相信这是因为我是一个新的意图每一次我打我的按钮启动,我想我需要使用标志,但我不能肯定。下面是我的基本MainActivity1和MainActivity2没有code我想,没有工作。

So on activity 1 I click a button that takes me to activity 2. In activity 2 I enter some data into an EditText. When I hit the back button on the phone it takes me to activity 1 which is correct but if I hit the activity 1 button again any text that I entered into the EditText is gone. I am sure this is because I am starting a new Intent every time I hit my button and I think I need to be using Flags but I am not certain. Below is my basic MainActivity1 and MainActivity2 without the code I tried that didn't work.

**编辑:退出所有保存的数据需要被删除的应用程序后,

** After exiting the app all saved data needs to be deleted.

MainActivity1

MainActivity1

public class MainActivity extends Activity {

Button b1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    b1 = (Button)findViewById(R.id.button2);
    b1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            Intent i = new Intent(MainActivity.this,MainActivity2.class);
            startActivity(i);


        }
    });
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

MainActivity2

MainActivity2

public class MainActivity2 extends Activity {


EditText et1;

@Override
protected void onCreate(Bundle outState) {
    super.onCreate(outState);
    setContentView(R.layout.activity_main_2);

    et1 = (EditText)findViewById(R.id.editText1);

    }
}

}

感谢你在前进。

推荐答案

试试这个复制这些codeS在MainActivity2这将节省您的数据(即你的EditText数据)

Try thiscopy these codes in your MainActivity2 it will save your data (i.e. your EditText Data)

public class MainActivity2 extends Activity {


EditText et1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_2);

et1 = (EditText)findViewById(R.id.editText1);
loadSavedPreferences();

}
private void loadSavedPreferences() {
SharedPreferences sharedPreferences = PreferenceManager
        .getDefaultSharedPreferences(this);

et1.setText(sharedPreferences.getString("string_et1",""));

}
private void savePreferences(String key, String value) {
SharedPreferences sharedPreferences = PreferenceManager
        .getDefaultSharedPreferences(this);
Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
public void saveData(){
savePreferences("string_et1", et1.getText().toString());
}
@Override
public void onBackPressed(){
    saveData();
super.onBackPressed();
}
}

如果你想删除preferences退出应用程序尝试在你的MainActivity1的时候。

if you want to delete the preferences when exiting the app try this in your MainActivity1.

public class MainActivity extends Activity {

Button b1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


b1 = (Button)findViewById(R.id.button2);
b1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

        Intent i = new Intent(MainActivity.this,MainActivity2.class);
        startActivity(i);


    }
});
}
@Override
public void onBackPressed() {
    clear_pref();

}
public void clear_pref(){
            SharedPreferences sharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(this);
            Editor editor = sharedPreferences.edit();
            editor.clear();
            editor.commit();
}
}

这篇关于打后退按钮时保存数据的EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 08:22