使用Eclipse保存Android应用程序的数据

时间:2012-04-03 14:54:10

标签: java android eclipse android-intent save

我已经完成了应用程序的布局,现在我只需要对它进行实际编程。我有一个“游戏菜单”,顶部有一个文本字段。此文本字段可以由位于不同页面上的EditText编辑,称为“团队详细信息”。当EditText中放入一个字符串时,该字符串将出现在“游戏菜单”顶部的文本字段中。这正是我想要发生的事情。但是,当我转到另一个页面并返回“游戏菜单”时,页面顶部的字符串已消失。我不知道如何让字符串永久保留在页面顶部,如果有人能帮助我,我会很高兴!

“团队详情”上的代码

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"));
        }
    });     


}
}

任何帮助都将受到极大的赞赏。谢谢。

3 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

如果用户可以编辑该字符串,并且您还希望它永久显示,则必须有一个保存机制。有关保存数据的各种方法的良好指南:

http://developer.android.com/guide/topics/data/data-storage.html

答案 2 :(得分:0)

也许,你想要这个

@Override
public void onResume() {
    getInput.setText(TeamName);
    textOut.setText(TeamName);
}