2 textviews总和 [英] a total sum of 2 textviews

查看:183
本文介绍了2 textviews总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private int ScoreCount;


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

    final TextView TotalScore = (TextView) findViewById(R.id.TotalScore);
    final TextView One = (TextView) findViewById(R.id.Score1);

    One.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            ScoreCount++;
            Front9.setText("" + ScoreCount);
        }
    });

如果我填的都textviews一个随机数,我希望2号在totalscore总和。
我怎么做。是code I加入到这样做的正确方法。
我知道onclicklistener是不是正确的方式,而是改用了什么。

If i fill a random number in both textviews, i want a sum of the 2 numbers in the totalscore. how do i do that. is the code i added the right way to do this. i know that the onclicklistener is not the right way but what to use instead.

推荐答案

我知道你在寻找的是一个TextWatcher的 http://developer.android.com/reference/android/text/TextWatcher.html

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

假设你有code采取递增每个分数TextViews的照顾,挂钩各TextViews的,象这样一个TextWatcher

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() {

    public void 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));
    }

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

    }

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

    }
});

修改 - 显然我误解了这个问题。为了增加每个分数,使用点击处理程序是一个可以接受的方法。见code以上完整的例子。忽略了意见和code以上。

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.

private int scoreTotal1;
private int scoreTotal2;
private int overallTotalScore;

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

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

    final TextView textViewtotalScore = (TextView) findViewById(R.id.TotalScore);
    final TextView textViewOne = (TextView) findViewById(R.id.Score1);
    final TextView textViewTwo = (TextView) findViewById(R.id.Score2);

    textViewOne.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            scoreTotal1++;
            overallTotalScore = scoreTotal1 + scoreTotal2;
            textViewOne.setText(String.valueOf(scoreTotal1));
            textViewTotalScore.setText(String.valueOf(overallTotalScore));
        }
    });

    textViewTwo.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            scoreTotal2++;
            overallTotalScore = scoreTotal1 + scoreTotal2;
            textViewTwo.setText(String.valueOf(scoreTotal2));
            textViewTotalScore.setText(String.valueOf(overallTotalScore));
        }
    });
}

这篇关于2 textviews总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆