点击后退按钮时将数据保存在edittext中 [英] Saving data in edittext when hitting Back button

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

问题描述

所以在活动 1 中,我单击一个按钮,将我带到活动 2.在活动 2 中,我将一些数据输入到 EditText 中.当我点击手机上的后退按钮时,它会将我带到活动 1,这是正确的,但如果我再次点击活动 1 按钮,我输入到 EditText 中的任何文本都消失了.我确信这是因为我每次按下按钮时都会开始一个新的 Intent,并且我认为我需要使用 Flags,但我不确定.下面是我的基本 MainActivity1 和 MainActivity2,没有我尝试过的代码.

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

    }
}

}

提前谢谢你.

推荐答案

试试这个将这些代码复制到您的 MainActivity2 中,它将保存您的数据(即您的 EditText 数据)

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

如果您想在退出应用时删除首选项,请在 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中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆