2012-04-03 27 views
0

我已經完成了我的應用程序的佈局,現在我只需要對其進行實際編程。我有一個「在遊戲菜單」,其頂部有一個文本字段。該文本字段可以通過位於不同頁面上的EditText編輯,稱爲「團隊詳細信息」。當EditText放入一個字符串時,該字符串出現在「In Game Menu」頂部的文本字段中。這正是我想要發生的事情。但是,當我轉到其他頁面並返回到「遊戲菜單」時,頁面頂部的字符串消失了。我不知道如何讓字符串永久保留在頁面頂部,如果有人能幫助我,我會喜歡它!使用Eclipse爲Android應用程序保存數據

代碼在 「團隊詳細信息」

package com.footballmanagerlog; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class teamDetails extends Activity{ 

TextView textOut; 
EditText getInput; 
String TeamName; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.teamdetails); 

    textOut = (TextView) findViewById(R.id.menuteamname); 
    getInput = (EditText) findViewById(R.id.NameTeam); 

    Button bbuttondone = (Button) findViewById(R.id.buttondone); 
    bbuttondone.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      TeamName = getInput.getText().toString();    
      Intent TeamNameIntent = new Intent("com.footballmanagerlog.GOTOINGAMEMENU");     
      TeamNameIntent.putExtra("TeamName", TeamName);    
      startActivity(TeamNameIntent); 
     } 
    }); 
} 
} 

和 「在遊戲菜單」

package com.footballmanagerlog; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class inGameMenu extends Activity{ 

TextView teamname; 
String teamnamestring; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.ingamemenu); 

    TextView teamname = (TextView) findViewById(R.id.menuteamname); 
    Intent teamnameintenttwo = getIntent(); 
    teamnamestring = teamnameintenttwo.getStringExtra("TeamName"); 
    teamname.setText(teamnamestring); 

    Button bbuttonmatch = (Button) findViewById(R.id.buttonmatch); 
    bbuttonmatch.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      startActivity(new Intent("com.footballmanagerlog.GOTOFORMATION")); 

     } 
    }); 

    Button bbuttonsubsidiaries = (Button) findViewById(R.id.buttonsubsidiaries); 
    bbuttonsubsidiaries.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      startActivity(new Intent("com.footballmanagerlog.GOTOSUBSIDIARIES")); 

     } 
    }); 

    Button bbuttonstatistics = (Button) findViewById(R.id.buttonstatistics);; 
    bbuttonstatistics.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      startActivity(new Intent("com.footballmanagerlog.GOTOSTATISTICS")); 

     } 
    }); 

    Button bbuttonteamdetails = (Button) findViewById(R.id.buttonteamdetails); 
    bbuttonteamdetails.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      startActivity(new Intent("com.footballmanagerlog.GOTOTEAMDETAILS")); 
     } 
    }); 

    Button bbuttonplayerdetails = (Button) findViewById(R.id.buttonplayerdetails); 
    bbuttonplayerdetails.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      startActivity(new Intent("com.footballmanagerlog.GOTOPLAYERDETAILS")); 
     } 
    });  


} 
} 

任何幫助將不勝感激的代碼。謝謝。

回答

1

您可以使用SharedPreferences數據存儲。

要保存的字符串:

SharedPreferences mypreferences = getSharedPreferences("myPrefs", Context.MODE_PRIVATE); 
SharedPreferences.Editor editor = mypreferences.edit(); 
editor.putString("stringName", "stringValue"); 
editor.commit(); 

要獲取字符串:

mypreferences = context.getSharedPreferences("myPrefs", Context.MODE_PRIVATE); 
String value = mypreferences.getString("stringName", "defaultValue"); 

在你的代碼:

**teamDetails** 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    TeamName = getInput.getText().toString(); 

    SharedPreferences mypreferences = getSharedPreferences("myPrefs", Context.MODE_PRIVATE); 
    SharedPreferences.Editor editor = mypreferences.edit(); 
    editor.putString("TeamName", TeamName); 
    editor.commit(); 

    Intent TeamNameIntent = new Intent("com.footballmanagerlog.GOTOINGAMEMENU");       
    startActivity(TeamNameIntent); 
} 

**inGameMenu** 
    setContentView(R.layout.ingamemenu); 
    TextView teamname = (TextView) findViewById(R.id.menuteamname); 

    SharedPreferences mypreferences = getApplicationContext().getSharedPreferences("myPrefs", Context.MODE_PRIVATE); 
    String teamnamestring = mypreferences.getString("TeamName", "no_name"); 

    teamname.setText(teamnamestring); 
+0

您好,感謝您的幫助。我嘗試這樣做,但有很多錯誤。我從一個類保存一個字符串,並試圖將其加載到另一個類中。有沒有可能把你的代碼放在我的正確位置?我不確定我需要做些什麼來說實話。抱歉。 – samwhoisconfused 2012-04-03 16:05:23

+0

我更新了我的答案 – Bakyt 2012-04-04 05:10:06

0

也許,你要想這

@Override 
public void onResume() { 
    getInput.setText(TeamName); 
    textOut.setText(TeamName); 
} 
相關問題