在android中,如何将两个按钮保存到同一个数据文件中?
我已经设置好了,所以当你单击其中一个单词时,它会添加 10 分,另一个单词会添加 5 分,但我希望它在同一个文本视图中输入,但是我不知道该怎么做,因为我如何现在它希望我将设置和编辑器更改为“settings1”和“editor1”,但如果我这样做,它将不再在“测试”TextView 中输入点数。我希望我将其扩展得足够好,感谢您的帮助,
这是我的代码
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;
}
}
}
I have it set up so when you click one of the words it will add 10 points and the other will add 5 points but I want it to be typed out in the same textview, however im not sure how to do it because how I have it right now it wants me to change the settings and editor to "settings1" and "editor1" but if I do that it will no longer type the amount of points out in the "test" TextView. I hope I expaned it good enough, thanks for helping
heres my code
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;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Case 语句不会创建新范围,因此您需要两次声明变量设置和编辑器。
除了添加的点数之外,两个 case 语句中的代码完全相同。为什么不创建一个包含所有共享代码的方法。
Case statements do not create a new scope, so you are declaring the variables settings and editor twice.
Your code in the two case statements are the exact same except for the number of points being added. Why not create a method that has all the shared code.