如何在Eclipse中保存多个EditTexts?

时间:2014-01-14 18:04:25

标签: java android eclipse edit

我正在制作一个类似app的时间表,供我个人使用。我想做的是:当我参加周一,周二,周三,周四,周五的课程时,我想保存这些课程。我找到了如何为一个EditText框做到这一点,但我每天都有5节课。所以目前我正在使用this example

我怎样才能完成所有5节课(p.s我正在使用保存按钮来保存课程。)

public class Week1_Mon extends Activity {


@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.week1_mon);
        editBox =(EditText)findViewById(R.id.week1_mon1);
        editBox1 =(EditText)findViewById(R.id.week1_mon2);



    }
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;



protected void onResume1() {
    super.onResume();
    SharedPreferences prefs = getPreferences(0); 
    String restoredText = prefs.getString("text", null);
    if (restoredText != null) {
        editBox1.setText(restoredText, TextView.BufferType.EDITABLE);

        int selectionStart = prefs.getInt("selection-start", -1);
        int selectionEnd = prefs.getInt("selection-end", -1);
        if (selectionStart != -1 && selectionEnd != -1) {
            editBox1.setSelection(selectionStart, selectionEnd);
        }
    }
}

protected void onPause1() {
    super.onPause();
    SharedPreferences.Editor editor = getPreferences(0).edit();
    editor.putString("text", editBox1.getText().toString());
    editor.putInt("selection-start", editBox1.getSelectionStart());
    editor.putInt("selection-end", editBox1.getSelectionEnd());
    editor.commit();
}

private EditText editBox1;

1 个答案:

答案 0 :(得分:0)

private void savePreferences(String key, String value){
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
    Editor e = sp.edit();
    e.putString(key, value);
    e.commit();
}


@Override
public void onClick(View v){
    savePreferences("Monday", editText1.getText().toString());
    savePreferences("Tuesday", editText2.getText().toString());
    savePreferences("Wednesday", editText3.getText().toString());
    savePreferences("Thursday", editText4.getText().toString());
    savePreferences("Friday", editText5.getText().toString());
}