Android : must write something before save

on Thursday, January 29, 2015


i have a edit text that i hopefully will be able to save soon, but to be able to save i have you would have to write something, i just need the code for checking if there is anything written in the edit text.


when i press the goback button i want to go back if i have not written anything, and i want to save if it has something written in it.



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

Button button = (Button)findViewById(R.id.GoBack);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
save();
Intent i = new Intent(anteckningsblock.this, Notelist.class);
startActivity(i);
}
});
}

void save() {
try {
File file = new File(getApplicationContext().getFilesDir(), "anteckning.txt");
FileOutputStream fos = new FileOutputStream(file);
DataOutputStream dos = new DataOutputStream(fos);
dos.writeUTF("Namnet på anteckningen");
dos.writeUTF("Innehållet");
dos.close();
} catch (Exception e) {
e.printStackTrace();
}
}

0 comments:

Post a Comment