通过textview(android studio)将整数传递给不同的活动以添加到主分数

ogsagwnx  于 2021-08-20  发布在  Java
关注(0)|答案(2)|浏览(320)

这是我在安卓工作室的第一个项目。代码有点乱。我正在为我和家人玩的纸牌游戏创建一个记分应用程序。
我在将我添加的分数(scorea2和scoreb2)从bid_活动转移到score_活动以添加到主分数文本视图时遇到问题,除非您按restart?
还有,一次点击=分数int,而双击=不同的分数。
score_活动java代码:

`public class score_activity extends AppCompatActivity {
      // creating an object of the text view
     TextView scoreA;
    TextView scoreB;
     TextView tvA;
     TextView tvB;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_score);
    // assigning the outputs of the user to the object
    scoreA = (TextView) findViewById(R.id.scoreA);
    scoreB = (TextView) findViewById(R.id.scoreB);
    tvA = (TextView) findViewById(R.id.nameA);
    tvB = (TextView) findViewById(R.id.nameB);
    Intent intent = getIntent();
    Bundle bundle = getIntent().getExtras();
    String text1 = bundle.getString("Team A");
    String text2 = bundle.getString("Team B");
    // setting the fetched data to the corresponding text views
    tvA.setText(text1);
    tvB.setText(text2);
    // this method is the logic that increases the value in the text 
     view by one on every click for team A
     }

    public void bidA(View view) {
    String nameA = tvA.getText().toString();
    String nameB = tvB.getText().toString();

    Intent intent = new Intent(score_activity.this, bid_activity.class);
    // creating an object of bundle
    Bundle bundle = new Bundle();
    // putting the inputs into the bundle
    bundle.putString("Team A", nameA);
    bundle.putString("Team B", nameB);

    intent.putExtras(bundle);
    // starting the activity
    startActivity(intent);
    }

    public void bidB(View view) {
    String nameA = tvA.getText().toString();
    String nameB = tvB.getText().toString();
    String scoreBoardA = scoreA.getText().toString();
    String scoreBoardB = scoreB.getText().toString();

    Intent myIntent = new Intent(score_activity.this, 
     activity_bidb.class);
    // creating an object of bundle
    Bundle bundle = new Bundle();
    // putting the inputs into the bundle
    bundle.putString("Team A", nameA);
    bundle.putString("Team B", nameB);
    bundle.putString("ScoreBoardA", scoreBoardA);
    bundle.putString("ScoreBoardB", scoreBoardB);

    myIntent.putExtras(bundle);
    // starting the activity
    startActivity(myIntent);
    }

    public void startAllOver(View view) {
    scoreA.setText("00");
    scoreB.setText("00");
     }

    }`

投标活动代码

public void scoreTotalB (View view){
        Bundle bundle = getIntent().getExtras();
        String text3 = bundle.getString("ScoreBoardA");
        String text4 = bundle.getString("ScoreBoardB");
        int scoreBa = Integer.parseInt(text3);
        int scoreBb = Integer.parseInt(text4);

        int scoreB2 = 0;
        String nameA = tvTeamA.getText().toString();
        String nameB = tvTeamB.getText().toString();
        int bidb = Integer.parseInt(bidB.getText().toString());
        int tricksTotalB = 
     Integer.parseInt(tricksB.getText().toString());
        int totalA = 25 - tricksTotalB;
        int ttotalA = mentionteamA + totalA;
        int totalB = mentionteamB + tricksTotalB;
        if (bidb >= totalB) {
            scoreB2 = -(totalB);
        }
        if (bidb <= totalB) {
            scoreB2 = totalB;
        }
        int totalScoreBA = scoreBa + scoreB2;
        int totalScoreBB = scoreBb + ttotalA;

        Intent i = new Intent(activity_bidb.this, 
      score_activity.class);
        i.putExtra("Team A", nameA);
        i.putExtra("Team B", nameB);
        i.putExtra("ScoreA2", totalScoreBA);
        i.putExtra("ScoreB2", totalScoreBB);

        i.putExtras(bundle);
        // starting the activity
       startActivityForResult(i, 0);

    }
li9yvcax

li9yvcax1#

一个想法是打电话

startActivityForResult(i)

而不是

startActivity(i)

然后,您可以在bidactivity中传递一个结果,并在ScoreActivity的onactivityresult调用中捕获它。

mzsu5hc0

mzsu5hc02#

我只是演示如何将您的投标活动中的数据传递给评分活动。
投标活动

Intent i = new Intent(bid_activity.this, score_activity.class);
i.putExtra("ScoreA2", scoreA2);
i.putExtra("ScoreB2", scoreB2);
startActivity(intent);

记分活动

int scoreA2 = getIntent().getIntExtra("ScoreA2");
int scoreB2 = getIntent().getIntExtra("scoreB2");

对于双击,您可以遵循此线程。

相关问题