[英]SetText doesn't change the TextView, how can I solve that?
我知道已經有一個這樣的問題,但是我無法通過答案解決問題,所以我要在這里提出問題,因為我的情況有所不同。 我的TextView不變。 我該如何解決?
package com.example.moresche.englishqigame;
public class scoreActivity extends AppCompatActivity {
int finalscorex;
int x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18,x19,x20,x21,x22,x23,x24,x25,x26,x27,x28,x29,x30;
int x31,x32,x33,x34,x35,x36,x37,x38,x39,x40,x41,x42,x43,x44,x45,x46,x47,x48,x49,x50;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_score);
}
public void OnStart(){
super.onStart();
}
TextView final_score = (TextView) findViewById(R.id.textView103);
public void initControls() {
final SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
int x1 = app_preferences.getInt("score1", 0);
int x2 = app_preferences.getInt("score2", 0);
int x3 = app_preferences.getInt("score3", 0);
...
int x48 = app_preferences.getInt("score4", 0);
int x49 = app_preferences.getInt("score49", 0);
int x50 = app_preferences.getInt("score50", 0);
finalscorex = x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10 + x11 + x12 + x13 + x14 + x15 + x16 + x17 + x18 + x19 + x20
+ x21 + x22 + x23 + x24 + x25 + x26 + x27 + x28 + x29 + x30 + x31 + x32 + x33 + x34 + x35 + x36 + x37 + x38 + x38 + x40
+ x41 + x42 + x43 + x44 + x45 + x46 + x47 + x48 + x49 + x50;
final_score.setText(finalscorex);
}
}
做這個
package com.example.moresche.englishqigame;
public class scoreActivity extends AppCompatActivity {
int finalscorex;
int[] x = new int[50];
TextView final_score;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_score);
final_score = (TextView) findViewById(R.id.textView103);
initControls();
}
public void initControls() {
final SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
String key;
for(int i = 0; i <= x.length; i++){
key = "score" + (i+1);
x[i] = app_preferences.getInt(key, 0);
finalscorex = finalScore + x[i];
}
final_score.setText(finalscorex);
}
}
話雖如此,您還應該研究android中的適當命名約定。 例如,應為TextView finalScore
和R.id.text_view_103
(也應反映在您的xml布局中)。
把你的
TextView final_score = (TextView) findViewById(R.id.textView103);
在您的onCreate
方法中。 您放置SharedPreferences
的方法在哪里?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.