0
私はそれを設定しているので、単語の1つをクリックすると10ポイントが追加され、もう1つは5ポイントが追加されますが、同じテキストビューでタイプされたいのですが、私は今、設定とエディタを "settings1"と "editor1"に変更したいと思っていますが、それを行うと "test" TextViewにポイントの量が入力されなくなります。私は、私は十分にそれが良いexpaned HERESに私のコードアンドロイドの同じデータファイルに保存するボタンは2つありますか?
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)); "設定"にエラーが発生しました –
@MichaelZeuner設定オブジェクトがまだありませんので、testScoreを設定することはできません。また、intを文字列に設定しようとしています。あなたができることは、変数をfinalでなく非staticにして、それをonCreateで設定することです。 –
私は残念ですが、これはあなたのボルネージですが、あなたが何をしているのかはかなりわかりませんが、文字列ではなくintでなければならないとおっしゃいましたか?文字列testScore =( "" + settings.getInt( "YourScore"、0));そうですね。また、私がしたことがある場合は、add pointsメソッドのtestScoreにエラーがあることがわかっています –