简体   繁体   English

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

[英]Why isn't my text saving when i leave the activity?

I have a notes app and basically i want it to save the text input and restore it when i return to the activity. 我有一个笔记应用程序,基本上我希望它保存文本输入并在我返回活动时恢复它。 But why isn't my code doing that? 但是我的代码为什么不这样做呢? I tried onRestoreInstancestate and onRestore but it's not working. 我尝试了onRestoreInstancestate和onRestore,但是它不起作用。 Any one know how i can save the text input after i exit the app? 有人知道我退出应用程序后如何保存文本输入吗?

notes.java: 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();
            }   
        });

    }
} 

Ok here you have the working, tested code 好了,这里有经过测试的有效代码

notes.java ( src\\izzy\\n\\notes.java ) notes.javasrc\\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 ) notes.xmlres\\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>

What you get 你得到什么

看起来如何

PS: You should maybe start reading some basic Java tutorials since you seem to have problems with basics :) PS:您可能应该开始阅读一些基本的Java教程,因为您似乎在基础方面有问题:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的数组不保存结果? - Why isn't my array saving the results? 为什么SharedPreferences不保存我的字符串? - Why isn't SharedPreferences saving my string? 从横向回到纵向时,为什么onSaveInstanceState无法正确保存我的EditText值 - Why isn't my onSaveInstanceState saving my EditText Value properly when going from landscape back to portrait 为什么在声明活动文件时没有将我的主要功能收起? - Why isn't my main function being picked up in my activity file when its declared? JAVA - 当我进入过滤器或排序离开时,为什么我的while循环不会停止? - JAVA - Why won't my while loop stop when I enter in filter or sort to leave it? 为什么我的int保存不正确? (java) - why isn't my int saving correctly? (java) 我的应用程序没有给我任何 output 并且当我单击下一个活动的按钮时崩溃? - My app isn't giving me any output and crashes when I click button for next activity? 为什么在使用SwingWorker时不下载图像,而在不使用SwingWorker时下载图像? - Why isn't my image downloading when I use a SwingWorker but downloads when I don't? 我无法弄清楚为什么我的程序在运行时没有打印 - I can't figure out why my program isn't printing when I run it 当我离开活动并返回活动时,活动不断重新开始 - Activity keeps restarting when I leave the activity and come back to it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM