我正在使用java在android中创建一个测验游戏,它的第一个活动包含一个表示所获得的点的TextView,显示存储在SharedPreference变量中的值,如果用户移动到另一个活动或玩游戏,该值会增加,但是第一个活动中的textview仍然显示初始值(仅当我重新启动应用程序时才显示最终值),如何在不使用startActivityforresult的情况下解决此问题
第一个活动代码
public void a_click(View ClickedButton)
{
int mode=Activity.MODE_PRIVATE;
SharedPreferences my=getSharedPreferences("alpha",mode);
String z=my.getString("correct","data_loss");
SharedPreferences data=getSharedPreferences("data",mode);
Integer coins=data.getInt("coins", 0);
if(z.compareTo("1")==0)
{
int mode66=Activity.MODE_PRIVATE;
SharedPreferences my66=getSharedPreferences("session",mode66);
int zedd=my66.getInt("qnum", 1);
int coinsP=0;
if(zedd==1)
{
coinsP=coins+5;
}
if(zedd==2)
{
coinsP=coins+5;
}
if(zedd==3)
{
coinsP=coins+10;
}
if(zedd==4)
{
coinsP=coins+15;
}
if(zedd==5)
{
coinsP=coins+20;
}
if(zedd==6)
{
coinsP=coins+25;
}
qnum++;
mugu(qnum);
SharedPreferences dataP=getSharedPreferences("data",mode);
SharedPreferences.Editor e2=dataP.edit();
e2.putInt("coins", coinsP);
e2.commit();
}
最终活动代码
<div class="container-fluid">
<ul class="list-inline">
<li class="selected_language">
<a href="/admin/Home/SetCulture?culture=et" title="et">
et
</a>
</li>
<li>
<a href="/admin/Home/SetCulture?culture=en" title="en">
en
</a>
</li>
</ul>
答案 0 :(得分:1)
覆盖resume方法并在textview中设置文本,如下所示。
@Override
public void onResume() {
super.onResume(); // Always call the superclass method first
textView.setText("YourText");
}
答案 1 :(得分:1)
调用startActivityforresult()
不是解决方案,因为它不适用于此。您应该做的只是更新onResume()
中的用户界面,当您的活动转到前台时(即当用户从其他活动中返回时)将调用该用户界面
@Override
public void onResume() {
super.onResume();
textView.setText(hiscore);
}
答案 2 :(得分:1)
public class MainProjectActivity extends Activity {
/** Called when the activity is first created. */
TextView displayScores;
static int Score = 0;
@Override
protected void onResume(){
super.onResume();
// update your data here
spSettings = getSharedPreferences(KEY,"VALUE");
displayScores.setText(spSettings.getString("YOUR KEY",
"YOURVALUE"));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Display Scores
displayScores = (TextView)findViewById(R.id.scoreDisplay);
displayScores.setText("Your Score : "+ Score);
//Play Game button activity
Button gameButton = (Button)findViewById(R.id.PlayButton);
gameButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent play = new Intent(getApplicationContext(), com.sample.game.PlayScreen.class);
startActivity(play);
}
});
答案 3 :(得分:1)
将此代码放入您的主要活动
Timer().scheduleAtFixedRate(new TimerTask() {
public void run() {
runOnUiThread(new Runnable() {
public void run() {
int mode = Activity.MODE_PRIVATE;
SharedPreferences data = getSharedPreferences("data", mode);
Integer coins = data.getInt("coins", 0);
String coinsString = coins.toString();
TextView coinView = (TextView) findViewById(R.id.coins);
coinView.setText(coinsString);
}
});
}
}, 0, 500);
此代码每0.5秒更新一次textview
答案 4 :(得分:0)
当您将数据存储在共享首选项中时,您可以为您的首选项添加侦听器,如下所示。
//Here you can register for changes in preference object
preferences.registerOnSharedPreferenceChangeListener(listener);
监听器实现如下
SharedPreferences.OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
// Implementation
tv.setText(prefs.getString(key, "defalut"));
}
};