gpt4 book ai didi

java - 为什么当我离开 Activity 时我的文本没有保存?

转载 作者:行者123 更新时间:2023-12-02 00:27:18 25 4
gpt4 key购买 nike

我有一个笔记应用程序,基本上我希望它保存文本输入并在我返回 Activity 时恢复它。但为什么我的代码没有这样做呢?我尝试了 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();
}
});

}
}

最佳答案

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

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 教程,因为您似乎在基础知识方面存在问题:)

关于java - 为什么当我离开 Activity 时我的文本没有保存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9750601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com