我一直在試圖創建一個應用程序,它可以保存您在文本框中輸入的文本,然後單擊按鈕進行保存,但每當我嘗試在應用程序中保存文本時,都無法這樣做。例如,我在文本框中輸入「Joe smith 123」,單擊保存按鈕,然後退出應用程序。一個完整的退出,甚至去標籤並從那裏刪除它。但是當我重新打開應用程序時,我輸入的文本不在那裏。這裏是xml和類文件的代碼。Android沒有保存文本?
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.rad.passwordstorage3.Storage">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SAVE PASSWORDS"
android:id="@+id/save"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="saveClicked"/>
<EditText
android:id="@+id/textbox"
android:singleLine="false"
android:gravity="top"
android:lines="10"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="none" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Enter your passwords in the textbox!"
android:id="@+id/textView"
android:layout_marginTop="41dp"
android:layout_below="@+id/save"
android:layout_centerHorizontal="true" />
</RelativeLayout>
類:
package com.example.rad.passwordstorage3;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Storage extends AppCompatActivity {
/** Called when the activity is first created. */
public void readFileInEditor()
{
try {
InputStream in = openFileInput(STORETEXT);
if (in != null) {
InputStreamReader tmp=new InputStreamReader(in);
BufferedReader reader=new BufferedReader(tmp);
String str;
StringBuilder buf=new StringBuilder();
while ((str = reader.readLine()) != null) {
buf.append(str+"n");
}
in.close();
txtEditor.setText(buf.toString());
}
}
catch (java.io.FileNotFoundException e) {
// that's OK, we probably haven't created it yet
}
catch (Throwable t) {
Toast
.makeText(this, "Exception: "+t.toString(), Toast.LENGTH_LONG)
.show();
}
}
private final static String STORETEXT="storetext.txt";
private EditText txtEditor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_storage);
txtEditor=(EditText)findViewById(R.id.textbox);
}
public void saveClicked(View v) {
try {
OutputStreamWriter out=
new OutputStreamWriter(openFileOutput(STORETEXT, 0));
out.write(txtEditor.getText().toString());
out.close();
Toast
.makeText(this, "Your passwords have been saved!", Toast.LENGTH_LONG)
.show();
}
catch (Throwable t) {
Toast
.makeText(this, "Exception: "+t.toString(), Toast.LENGTH_LONG)
.show();
}
}
}
有任何問題,我的保存方法,還是其他什麼東西? 謝謝。
當我刪除所有這些,代碼混亂,我得到很多錯誤。我應該在onCreate方法中調用它嗎?如果是這樣,怎麼樣? –
然後在onCreate();只需調用readFileInEditor(); –
您的代碼可以正常工作,並且您輸入的文字在應用重新打開時會顯示。其實,我正在測試你的代碼,它的作用就像一個魅力:)!問題在於,你將整個代碼封裝在一個方法中,而當應用程序(重新)打開時,你沒有在onCreate()中調用該方法。 –