Please look at my code. As you see it has one editText and what it does- it saves text in editText .I found this code online. It works perfectly with my layout. But I added another editText box, that I called editText2 and can not figure out how to code it. How to make text in the second one to be saved also? Do I need to create a new class in src? I get that I have to add editText2 and editBox2 , but how and where? Can someone give me just one sample, so I could use it for the whole code? For example, on protected void onCreate- how do I add editText2?Thanks!
package tryone.now.forfreenow;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
public class notepad extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editBox =(EditText)findViewById(R.id.editText1);
}
protected void onResume() {
super.onResume();
SharedPreferences prefs = getPreferences(0);
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
editBox.setText(restoredText, TextView.BufferType.EDITABLE);
int selectionStart = prefs.getInt("selection-start", -1);
int selectionEnd = prefs.getInt("selection-end", -1);
if (selectionStart != -1 && selectionEnd != -1) {
editBox.setSelection(selectionStart, selectionEnd);
}
}
}
protected void onPause() {
super.onPause();
SharedPreferences.Editor editor = getPreferences(0).edit();
editor.putString("text", editBox.getText().toString());
editor.putInt("selection-start", editBox.getSelectionStart());
editor.putInt("selection-end", editBox.getSelectionEnd());
editor.commit();
}
private EditText editBox;
}