【问题标题】:Saving data for an Android application using Eclipse使用 Eclipse 为 Android 应用程序保存数据
【发布时间】:2012-04-03 14:54:10
【问题描述】:

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

“团队详情”上的代码

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


}
}

任何帮助将不胜感激。谢谢。

【问题讨论】:

    标签: java android eclipse android-intent save


    【解决方案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);
    

    【讨论】:

    • 嗨,感谢您的帮助。我试过这样做,但有很多错误。我从一个类中保存一个字符串并尝试将其加载到另一个类中。你有没有机会把你的代码放在我的正确位置?我不确定我需要做什么才能说实话。对不起。
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      也许,你想要这个

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-30
        • 2016-12-15
        • 1970-01-01
        相关资源
        最近更新 更多