我正在努力解决如何使用edittext将更改保存到我的file.txt。到目前为止,我的代码将允许我打开文本文件,但是在关闭它时,它不会保存所做的更改。文本文件正被打开到另一个活动中,并且在方向改变和最小化时将保存更改。我已经尝试了很多不同的解决方案,但我无法理解如何存储所做的更改。
public class Editor extends Activity {
private String Text;
private String Folder;
private String toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_editor);
Bundle extras = getIntent().getExtras();
this.Text = extras.getString ("txt");
this.Folder = extras.getString("s");
this.toast = (Folder + "/" + Text);
Toast.makeText(this, toast, Toast.LENGTH_SHORT).show();
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard + "/NoteTaker/" + Folder + "/" + Text);
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
}
catch (IOException e) {
}
TextView tv = (TextView)findViewById(R.id.editText1);
tv.setText(text);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_editor, menu);
return true;
}