在android中,如何将两个按钮保存到同一个数据文件中?

发布于 2025-01-03 14:00:42 字数 1730 浏览 0 评论 0原文

我已经设置好了,所以当你单击其中一个单词时,它会添加 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十年九夏 2025-01-10 14:00:42

Case 语句不会创建新范围,因此您需要两次声明变量设置和编辑器。

除了添加的点数之外,两个 case 语句中的代码完全相同。为什么不创建一个包含所有共享代码的方法。

private void addPoints(int points) {
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putInt("YourScore", (testScore + points));
    editor.commit();       
}

public void onClick(View v) 
{
    switch(v.getId())
    {
    case R.id.Q1A1:
        addPoints(10);
        break;
    case R.id.Q1A2:
        addPoints(5);
        break;
    }   
}   

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.

private void addPoints(int points) {
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putInt("YourScore", (testScore + points));
    editor.commit();       
}

public void onClick(View v) 
{
    switch(v.getId())
    {
    case R.id.Q1A1:
        addPoints(10);
        break;
    case R.id.Q1A2:
        addPoints(5);
        break;
    }   
}   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文