请查看我的代码。如你所见,它有一个editText及其功能 - 它在editText中保存文本。我在网上找到了这个代码。它与我的布局完美配合。但是我添加了另一个editText框,我调用了editText2并且无法弄清楚如何对其进行编码。如何在第二个文本中保存文本呢?我需要在src中创建一个新类吗?我知道我必须添加editText2和editBox2,但是如何以及在哪里?有人可以给我一个样本,所以我可以将它用于整个代码吗?例如,on protected void onCreate-如何添加editText2?谢谢!
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)
您需要了解有关布局和UI小部件的一些基础知识。 editBox1
在main.xml
目录下的res\layout
中定义。打开该文件并查看editBox1
,复制代码并将id更改为editBox2。然后在onCreate()
中添加代码editBox2 =(EditText)findViewById(R.id.editText2)
;
获得参考
答案 1 :(得分: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;
}