当我离开活动时为什么不保存我的文字?

时间:2012-03-17 14:08:03

标签: java android

我有一个笔记应用程序,基本上我希望它保存文本输入并在我返回活动时恢复它。但为什么我的代码不这样做呢?我试过onRestoreInstancestate和onRestore,但它没有用。退出应用程序后,任何人都知道如何保存文本输入?

notes.java:

public class notes extends Activity{




    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notes);




        Button wg = (Button) findViewById(R.id.button3);
        wg.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent intent = new Intent();
                setResult(RESULT_OK, intent);
                finish();
            }   
        });

    }
} 

1 个答案:

答案 0 :(得分:3)

好的,你有经过测试的工作代码

notes.java src\izzy\n\notes.java

package izzy.n;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

// actually you should name the class "Notes" since it's convention
// but it would proably cause errors since you have to rename the file then.
public class notes extends Activity {
    public static final String DEFAULT_TEXT = "Write some text here";
    public static final String PREFS_KEY_TEXT = "text";
    public static final String PREFS_NAME = "MyPrefsFile";

    private EditText mEditText;

    /**
     * Loads the text from SharedPreferences
     */
    private String loadText() {
        SharedPreferences settings = getSharedPreferences(notes.PREFS_NAME,
                Context.MODE_PRIVATE);
        return settings.getString(notes.PREFS_KEY_TEXT, notes.DEFAULT_TEXT);
    }

    /**
     * Saves text to SharedPreferences
     */
    private void saveText(String text) {
        SharedPreferences settings = getSharedPreferences(notes.PREFS_NAME,
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(notes.PREFS_KEY_TEXT, text);
        editor.commit();
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notes);

        // when you click on the button the app will exit
        // you kind of don't need such a button :)
        Button wg = (Button) findViewById(R.id.exit_button);
        wg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        mEditText = (EditText) findViewById(R.id.edit_text);

        String savedText = loadText();
        mEditText.setText(savedText);
        // put the cursor to the end.
        mEditText.setSelection(savedText.length());
    }

    @Override
    protected void onStop() {
        super.onStop();
        saveText(mEditText.getText().toString());
    }

}

notes.xml res\layout\notes.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/exit_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Exit" />

    <EditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:gravity="top"
        android:layout_weight="1"
        android:hint="Enter Text here"
        android:inputType="textMultiLine" />

</LinearLayout>

你得到什么

how it looks

PS:您应该开始阅读一些基本的Java教程,因为您似乎遇到了基础问题:)