如何跟踪 Android 应用程序中的变量?

发布于 2025-01-12 20:39:00 字数 4037 浏览 0 评论 0原文

当用户进入应用程序时,他选择了球队1(巴西)或球队2(阿根廷),然后他选择了黄牌或红牌,然后他选择了一名球员,然后在第一页中会显示球员号码和卡片颜色。

在此处输入图像描述

我的问题是如何跟踪号码卡片数量,而不是显示玩家编号,而是显示球队有多少张黄牌或红牌。

CardActivity

public class CardActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_card);
}

private void setCardData(int card) {
    SharedPreferences settings getApplicationContext().getSharedPreferences("MainThisApp", 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putInt("card", card);
    editor.apply();

    Intent intent = new Intent(this, Myapp.game.NumberActivity.class);
    startActivity(intent);
}

public void setRedCard(View view) {

    ((Variable) this.getApplication()).setSomeVariable("card_yellow");

    setCardData(0);
}

public void setYellowCard(View view) {

    ((Variable) this.getApplication()).setSomeVariable("card_red");

    setCardData(1);
}}

MainActivity

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    SharedPreferences prefs = getApplicationContext().getSharedPreferences("MainThisApp", 0);
    int check = prefs.getInt("check", 0);

    String s = ((Variable) this.getApplication()).getSomeVariable();
    try{
        Integer number = Integer.valueOf(s);
        System.out.println(number);
    }
    catch (NumberFormatException ex){
        ex.printStackTrace();
    }

    if (check == 50) {
        TextView view = null;
        int country = prefs.getInt("country", -1);
        int card = prefs.getInt("card", -1);

        if (country == 0 & card == 0) {

            view = findViewById(R.id.textView4);
        }
        if (country == 0 & card == 1) {

            view = findViewById(R.id.textView3);
        }
        if (country == 1 & card == 0) {

            view = findViewById(R.id.textView2);

        }
        if (country == 1 & card == 1) {

            view = findViewById(R.id.textView);
        }
        view.setText(String.valueOf(prefs.getInt("number",1)));
    }

}

public void chooseBrasil(View view) {
    setCountryData(0);
}

private void setCountryData(int country) {
    SharedPreferences settings = getApplicationContext().getSharedPreferences("MainThisApp", 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putInt("country", country);
    editor.apply();
    Intent intent = new Intent(this, CardActivity.class);
    startActivity(intent);
}
public void chooseArgentine(View view) {
    setCountryData(1);
}

NumberActivity< /强>

public class NumberActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_number);
    }

private void setNumberData(int number) {
    SharedPreferences settings = getApplicationContext().getSharedPreferences("MainThisApp", 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putInt("number", number);
    editor.putInt("check", 50);
    editor.apply();
    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);
}

public void setNumber1(View view) {
    setNumberData(1);
}

public void setNumber2(View view) {
    setNumberData(2);
}

public void setNumber3(View view) {
    setNumberData(3);
}

public void setNumber4(View view) {
    setNumberData(4);
}

public void setNumber5(View view) {
    setNumberData(5);
}

public void setNumber6(View view) {
    setNumberData(6);
}

public void setNumber7(View view) {
    setNumberData(7);
}

public void setNumber8(View view) {
    setNumberData(8);
}

when user enter to the app he chose team 1 (Brazil)or team 2(Argentine) after that he chose yellow or red card then he chose a player after that in the first page it will show the player number and color of card.

enter image description here

My Question is How can keep track of the number of card and instead of showing player number I show how many either yellow or red card does team have .

CardActivity

public class CardActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_card);
}

private void setCardData(int card) {
    SharedPreferences settings getApplicationContext().getSharedPreferences("MainThisApp", 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putInt("card", card);
    editor.apply();

    Intent intent = new Intent(this, Myapp.game.NumberActivity.class);
    startActivity(intent);
}

public void setRedCard(View view) {

    ((Variable) this.getApplication()).setSomeVariable("card_yellow");

    setCardData(0);
}

public void setYellowCard(View view) {

    ((Variable) this.getApplication()).setSomeVariable("card_red");

    setCardData(1);
}}

MainActivity

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    SharedPreferences prefs = getApplicationContext().getSharedPreferences("MainThisApp", 0);
    int check = prefs.getInt("check", 0);

    String s = ((Variable) this.getApplication()).getSomeVariable();
    try{
        Integer number = Integer.valueOf(s);
        System.out.println(number);
    }
    catch (NumberFormatException ex){
        ex.printStackTrace();
    }

    if (check == 50) {
        TextView view = null;
        int country = prefs.getInt("country", -1);
        int card = prefs.getInt("card", -1);

        if (country == 0 & card == 0) {

            view = findViewById(R.id.textView4);
        }
        if (country == 0 & card == 1) {

            view = findViewById(R.id.textView3);
        }
        if (country == 1 & card == 0) {

            view = findViewById(R.id.textView2);

        }
        if (country == 1 & card == 1) {

            view = findViewById(R.id.textView);
        }
        view.setText(String.valueOf(prefs.getInt("number",1)));
    }

}

public void chooseBrasil(View view) {
    setCountryData(0);
}

private void setCountryData(int country) {
    SharedPreferences settings = getApplicationContext().getSharedPreferences("MainThisApp", 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putInt("country", country);
    editor.apply();
    Intent intent = new Intent(this, CardActivity.class);
    startActivity(intent);
}
public void chooseArgentine(View view) {
    setCountryData(1);
}

NumberActivity

public class NumberActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_number);
    }

private void setNumberData(int number) {
    SharedPreferences settings = getApplicationContext().getSharedPreferences("MainThisApp", 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putInt("number", number);
    editor.putInt("check", 50);
    editor.apply();
    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);
}

public void setNumber1(View view) {
    setNumberData(1);
}

public void setNumber2(View view) {
    setNumberData(2);
}

public void setNumber3(View view) {
    setNumberData(3);
}

public void setNumber4(View view) {
    setNumberData(4);
}

public void setNumber5(View view) {
    setNumberData(5);
}

public void setNumber6(View view) {
    setNumberData(6);
}

public void setNumber7(View view) {
    setNumberData(7);
}

public void setNumber8(View view) {
    setNumberData(8);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

断爱 2025-01-19 20:39:00

您可以使用 ROOM 数据库或 共享首选项

或者只是使用 kotlin (Singelton) 中的对象来保存可以在任何地方获取和设置的对象中的任何数据

You can use ROOM database or Shared preferences.

Or just use object in kotlin (Singelton) to save any data in Object that you can get and set everywhere

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文