Saving data in edittext when hitting Back button(点击后退按钮时将数据保存在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中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:点击后退按钮时将数据保存在edittext中
基础教程推荐
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01