我有一个EditText的代码,但我无法弄清楚如何为第二个editText Box制作相同的代码,它位于同一页面上。这是我的代码:
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;
}
答案 0 :(得分:0)
我正在回答我自己的问题。它可以100%在两个editText框上保存状态。
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;
}