[英]How can I update text of android textview
I am creating a quiz game in android using java, Its first activity contains a TextView that represents the points earned ,displays the value stored in SharedPreference variable, This value is increasing if the user moves to another activities or playing game,But the textview in first activity still displays the initial value (It displays the final value only if i restart application), How can I solve this issue without using startActivityforresult 我正在使用Java在android中创建测验游戏,其第一个活动包含一个TextView,代表所赚取的积分,显示存储在SharedPreference变量中的值,如果用户移至其他活动或玩游戏,则该值会增加,但是textview在第一个活动仍显示初始值(仅当我重新启动应用程序时,它才显示最终值),如何在不使用startActivityforresult的情况下解决此问题
First activity code 首次活动代码
int mode4 = Activity.MODE_PRIVATE;
SharedPreferences data = getSharedPreferences("data", mode4);
Integer coins = data.getInt("coins", 0);
String coinsString = coins.toString();
TextView coinView = (TextView) findViewById(R.id.coins);
coinView.setText(coinsString);
Final activity code 最终活动代码
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();
}
Override the resume method and set text in textview there as show below. 覆盖简历方法,并在textview中设置文本,如下所示。
@Override
public void onResume() {
super.onResume(); // Always call the superclass method first
textView.setText("YourText");
}
Calling startActivityforresult()
is not a solution as it is not for this. 调用
startActivityforresult()
不是解决方案,因为它不是为此。 What you should do is simply update your UI in onResume()
which will be called whenver your Activity goes to foreground (ie when user gets back from other activities) 您应该做的就是简单地在
onResume()
更新UI,无论何时您的Activity进入前台(即,当用户从其他活动中回来时onResume()
,都会调用该UI
@Override
public void onResume() {
super.onResume();
textView.setText(hiscore);
}
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);
}
});
put this code in your mainactivity 将此代码放入您的mainactivity中
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);
this code will update your textview for every 0.5 seconds 该代码将每0.5秒更新一次textview
As you are storing the data in Shared Preference so you can add listener for your preferences, as follows. 当您将数据存储在“共享首选项”中时,您可以如下添加首选项的侦听器。
//Here you can register for changes in preference object
preferences.registerOnSharedPreferenceChangeListener(listener);
Listener implementation will be as follows 侦听器的实现如下
SharedPreferences.OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
// Implementation
tv.setText(prefs.getString(key, "defalut"));
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.