2016-07-29 45 views
0

我一直在試圖創建一個應用程序,它可以保存您在文本框中輸入的文本,然後單擊按鈕進行保存,但每當我嘗試在應用程序中保存文本時,都無法這樣做。例如,我在文本框中輸入「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(); 

     } 

    } 
} 

有任何問題,我的保存方法,還是其他什麼東西? 謝謝。

回答

1

這, 就在onCreate()方法添加此行如下:

readFileInEditor();

就是這樣。你是在一個方法來包裝一下你的代碼,也許你只是忘記它調用onCreate()方法:

並在代碼中的註釋這行不正確的:

/**調用時的活動第一次創建。 */

因爲在應用程序啓動時首先調用的方法不是您放在第一位的代碼(或代碼塊),而是您使用單詞「@Override」覆蓋的onCreate()方法。請享用!

+0

當我刪除所有這些,代碼混亂,我得到很多錯誤。我應該在onCreate方法中調用它嗎?如果是這樣,怎麼樣? –

+0

然後在onCreate();只需調用readFileInEditor(); –

+0

您的代碼可以正常工作,並且您輸入的文字在應用重新打開時會顯示。其實,我正在測試你的代碼,它的作用就像一個魅力:)!問題在於,你將整個代碼封裝在一個方法中,而當應用程序(重新)打開時,你沒有在onCreate()中調用該方法。 –

3

當你完成並關閉你的應用程序,它的狀態在那一刻消失。如果要恢復該文本,則需要在onCreate方法中再次加載'.txt'。不過,如果文本不是太長,我建議你使用SharedPreferences而不是OSW,因爲它比較容易實現,它使用的系統資源較少。

這裏有更多的信息: Shared Preferences

+0

我會使用共享偏好設置,但我處於時間限制,現在無法修復它,此外,文本太大,因爲文本框必須包含多個項目。有一段代碼是用來調用文件中的文本(public void read file editor),但這不起作用。我如何解決它? –

相關問題