我有一个应用程序,其中单击图像使值(goldCount)增加1.运行时,应用程序在启动应用程序之前运行启动。但是,如果我最小化或关闭游戏,goldCount值将恢复为0.0,并且关于SharedPreferences的教程没有提供关于如何保存浮点值的线索。
我的主要Java代码:
package com.bipbapapps.leagueclickerapp;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
public class MainClass extends Activity implements OnClickListener {
public float goldCount = 0.0f;
Button minionClick;
TextView textGoldCount;
String textTotal;
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Set fullscreen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.mainlayout);
//Linking the variables
minionClick = (Button) findViewById(R.id.minioncentreid);
textGoldCount = (TextView) findViewById(R.id.textviewtop);
//String which will display at the top of the app
textTotal = goldCount + " Gold";
//Setting TextView to the String
textGoldCount.setText(textTotal);
textGoldCount.setGravity(Gravity.CENTER);
Typeface tf = Typeface.createFromAsset(getAssets(), "mechanical.ttf");
textGoldCount.setTypeface(tf);
textGoldCount.setTextSize(35);
//Setting onClickListener
minionClick.setClickable(true);
minionClick.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.minioncentreid:
goldCount += 1.0;
textTotal = goldCount + " Gold";
textGoldCount.setText(textTotal);
textGoldCount.setGravity(Gravity.CENTER);
break;
}
}
}
我的Splash代码,如果它是相关的:
package com.bipbapapps.leagueclickerapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
public class Splash extends Activity {
@Override
public void onCreate(Bundle splashBundle) {
// TODO Auto-generated method stub
super.onCreate(splashBundle);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splash);
Thread logoTimer = new Thread(){
public void run(){
try {
sleep(2000);
Intent mainIntent = new Intent("com.bipbapapps.leagueclickerapp.CLICKER");
startActivity(mainIntent);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
finish();
}
}
};
logoTimer.start();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
如果有人能帮助我,我将非常感激。
答案 0 :(得分:0)
与您的问题无关,但在Splash
代码中,请执行以下操作,而不是创建新主题:
new Handler().postDelayed(new Runnable() {
public void run() {
Intent mainIntent = new Intent("com.bipbapapps.leagueclickerapp.CLICKER");
startActivity(mainIntent);
}
}, 2000);
它更清洁,在定义延迟动作的大多数情况下是首选。
要回答您的问题,您似乎没有处理任何事实,以实际保存goldCount
的价值。
在课程顶部执行以下操作。 " APP_NAME_HERE"应该用您的应用程序名称或任何类型的密钥替换。
SharedPreferences prefs = getSharedPreferences("APP_NAME_HERE", Context.MODE_PRIVATE);
在onCreate方法中,执行以下操作:
public void onCreate(Bundle savedInstanceState) {
// other onCreateStuff
goldCount = prefs.getFloat("goldCount", 0.0f);
// other onCreateStuff
}
然后在你的onClick
方法中:
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.minioncentreid:
goldCount += 1.0;
prefs.edit().putFloat("goldCount", goldCount).commit();
textTotal = goldCount + " Gold";
textGoldCount.setText(textTotal);
textGoldCount.setGravity(Gravity.CENTER);
break;
}
}