2012-03-17 28 views
0

我有一個筆記應用程序,基本上我希望它可以保存文本輸入並在我返回活動時恢復它。但爲什麼我的代碼不是這樣做的?我嘗試過恢復實例狀態和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(); 
      } 
     }); 

    } 
} 
+2

的問題,如果你'完成()'你告訴Android,你的應用不應該被恢復。也沒有真正保存文本的代碼。看到[SharedPreferences](http://developer.android.com/reference/android/content/SharedPreferences.html)左右 – zapl 2012-03-17 14:10:29

+0

所以我把它拿出來,並放置super.onRestore()? – user1248404 2012-03-17 14:11:34

+0

你在哪裏保存你的文字? – Deepak 2012-03-17 14:16:51

回答

3

確定這裏你已經工作,測試代碼

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.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> 

你得到什麼

how it looks

PS:你也許應該開始讀一些基本的Java教程因爲你似乎有基礎:)

+0

我收到很多錯誤與你提供的我 – user1248404 2012-03-17 16:40:54

+0

這不是一個完整的代碼示例,並直接在這個編輯器中編寫 - 它甚至可能包含語法錯誤等,你需要調整它,以便它適合你。例如。你必須在'onCreate'中添加你所做的代碼,而我必須確保在'notes.xml'中有一個帶有'android:id =「@ + id/edit_text1」'的EditText。如果你比「很多錯誤」更具體,我可以幫助你更多。 – zapl 2012-03-17 16:45:54

+0

我會做我現在可以調整它到我的代碼,我會馬上回到你;) – user1248404 2012-03-17 16:50:34

相關問題