0
我設置了它,所以當你點擊其中一個單詞時,它會增加10點,另一個會增加5點,但我希望它在同一個textview中輸入,但我不知道如何去做,因爲如何我現在擁有它,它希望我將設置和編輯器更改爲「settings1」和「editor1」,但如果我這樣做,它將不再在「測試」TextView中輸入點數。我希望我用不了兩年時間就不夠好,感謝幫助你如何有兩個按鈕保存到android中的相同數據文件?
繼承人我的代碼
public class page1 extends Activity implements OnClickListener
{
TextView Q1A1;
TextView Q1A2;
TextView test;
public static final String PREFS_NAME = "MyPrefsFile";
public static final int testScore = 0;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
Q1A1 = (TextView) findViewById(R.id.Q1A1);
Q1A2 = (TextView) findViewById(R.id.Q1A2);
Q1A1.setOnClickListener(this);
Q1A2.setOnClickListener(this);
test = (TextView) findViewById(R.id.test);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
test.setText(""+settings.getInt("YourScore", 0));
}
public void onClick(View v)
{
switch(v.getId())
{
case R.id.Q1A1:
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("YourScore", (testScore + 10));
editor.commit();
//Intent FinalScore = new Intent(this, FinalScore.class);
//startActivity(FinalScore);
break;
case R.id.Q1A2:
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("YourScore", (testScore + 5));
editor.commit();
break;
}
}
}
非常感謝! 你會碰巧知道如何擁有它,所以TestScore等於你的得分? 我有什麼是 public static final int testScore =(「」+ settings.getInt(「YourScore」,0)); 但我在「設置」上遇到錯誤 – 2012-02-06 20:08:58
@MichaelZeuner您還沒有設置對象,所以您無法設置testScore。你也試圖設置一個int爲一個字符串。你可以做的是使變量非終極和非靜態,然後將其設置在onCreate中。 – 2012-02-06 20:19:59
對不起,如果這是對你的支持,但林不知道你是什麼,你說它應該是一個字符串,而不是一個int? String testScore =(「」+ settings.getInt(「YourScore」,0));像那樣。也有一次我這樣做,我知道有一個testScore在添加點方法 – 2012-02-06 20:35:48