Skip to content Skip to sidebar Skip to footer

A Total Sum Of 2 Textviews

private int ScoreCount; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

Solution 1:

I think what you are looking for is a TextWatcher http://developer.android.com/reference/android/text/TextWatcher.html

Assuming you have code to take care of incrementing each of your score TextViews, hook up each of the TextViews with a TextWatcher like so

One.addTextChangedListener(new TextWatcher() {

    publicvoid onTextChanged(CharSequence s, int start, int before, int count) {
        int score1 = Integer.parseInt(One.getText());
        int score2 = Integer.parseInt(Two.getText());
        FrontNine.setText(String.valueOf(score1 + score2));
    }

    publicvoid beforeTextChanged(CharSequence s, int start, int count, int after) {
        // TODO Auto-generated method stub

    }

    publicvoid afterTextChanged(Editable s) {
        // TODO Auto-generated method stub

    }
});

Edit - Apparently I misunderstood the question. To increment each of the scores, using a click handler is an acceptable approach. See code above for the complete example. Disregard the comments and code above.

privateint scoreTotal1;
privateint scoreTotal2;
privateint overallTotalScore;

/** Called when the activity is first created. */@OverridepublicvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.score);

    scoreTotal1 = 0;
    scoreTotal2 = 0;
    overallTotalScore = 0;

    finalTextViewtextViewtotalScore= (TextView) findViewById(R.id.TotalScore);
    finalTextViewtextViewOne= (TextView) findViewById(R.id.Score1);
    finalTextViewtextViewTwo= (TextView) findViewById(R.id.Score2);

    textViewOne.setOnClickListener(newOnClickListener() {
        publicvoidonClick(View v) {
            scoreTotal1++;
            overallTotalScore = scoreTotal1 + scoreTotal2;
            textViewOne.setText(String.valueOf(scoreTotal1));
            textViewTotalScore.setText(String.valueOf(overallTotalScore));
        }
    });

    textViewTwo.setOnClickListener(newOnClickListener() {
        publicvoidonClick(View v) {
            scoreTotal2++;
            overallTotalScore = scoreTotal1 + scoreTotal2;
            textViewTwo.setText(String.valueOf(scoreTotal2));
            textViewTotalScore.setText(String.valueOf(overallTotalScore));
        }
    });
}

Post a Comment for "A Total Sum Of 2 Textviews"