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("sele开发者_开发知识库ction-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;
}
You need to learn some basics about layout and UI widgets. The editBox1
is defined in the main.xml
under res\layout
directory. Open that file and take a look at the editBox1
, copy the code and change the id to editBox2. Then in the onCreate()
add the code editBox2 =(EditText)findViewById(R.id.editText2)
;
to get the reference
I am answering my own question.It works 100% to save state on two editText boxes.
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);
editBox2 =(EditText)findViewById(R.id.editText2);
}
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);
}
SharedPreferences prefs2 = getPreferences(1);
String restoredText2 = prefs2.getString("text2", null);
if (restoredText2 != null)
{
editBox2.setText(restoredText2, TextView.BufferType.EDITABLE);
int selectionStart2 = prefs2.getInt("selection-start2", -1);
int selectionEnd2 = prefs2.getInt("selection-end2", -1);
if (selectionStart2 != -1 && selectionEnd2 != -1)
{
editBox2.setSelection(selectionStart2, selectionEnd2);
}
}
}
}
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();
SharedPreferences.Editor editor2 = getPreferences(1).edit();
editor2.putString("text2", editBox2.getText().toString());
editor2.putInt("selection-start2", editBox2.getSelectionStart());
editor2.putInt("selection-end2", editBox2.getSelectionEnd());
editor2.commit();
}
private EditText editBox;
private EditText editBox2;
}
精彩评论