我要创建两个按钮,点击者加起来不同号码 [英] I want to create two button clickers that add up different numbers

查看:94
本文介绍了我要创建两个按钮,点击者加起来不同号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我preSS一个按钮,然后preSS另一方面,金额上都TextViews加起来。我想他们是独立的数字。我试图谷歌找到一个解决办法,但我无法找到任何东西。请帮助?

 公共类MainActivity延伸活动{    INT点击= 0;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        // TODO自动生成方法存根
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.layout_main);
            {
                最后的TextView文本=(TextView的)findViewById(R.id.textView2);
                最终的ImageButton按钮=(的ImageButton)findViewById(R.id.imageButton2);
                button.setOnClickListener(新View.OnClickListener(){                    @覆盖
                    公共无效的onClick(视图v){
                        // TODO自动生成方法存根
                        点击++;
                        text.setText(+点击+);                    }
                });
            }            最后的TextView文本=(TextView的)findViewById(R.id.textView1);
            text.setText();
            最终的ImageButton按钮=(的ImageButton)findViewById(R.id.imageButton1);
            button.setOnClickListener(新View.OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                // TODO自动生成方法存根
                点击++;
                text.setText(+点击+);            }
        });    }}


解决方案

使用两个不同的变量。单击的被加入,当你preSS任一按钮来。同时选择适合您的按钮名称不同,不命名他们两个按钮。

例如:

 公共类MainActivity延伸活动{INT clicked1 = 0;
INT clicked2 = 0;
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    // TODO自动生成方法存根
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.layout_main);
        {
            最后的TextView文本=(TextView的)findViewById(R.id.textView2);
            最终的ImageButton按钮2 =(的ImageButton)findViewById(R.id.imageButton2);
            button.setOnClickListener(新View.OnClickListener(){                @覆盖
                公共无效的onClick(视图v){
                    // TODO自动生成方法存根
                    clicked2 ++;
                    text.setText(+ clicked2 +);                }
            });
        }        最后的TextView文本=(TextView的)findViewById(R.id.textView1);
        text.setText();
        最终的ImageButton按钮1 =(的ImageButton)findViewById(R.id.imageButton1);
        button.setOnClickListener(新View.OnClickListener(){        @覆盖
        公共无效的onClick(视图v){
            // TODO自动生成方法存根
            clicked1 ++;
            text.setText(+ clicked1 +);        }
    });}}

If i press one button and then press the other, the amount adds up on both TextViews. I want them to be separate numbers. I tried google to find a solution but i couldn't find anything. Please help?

public class MainActivity extends Activity {

    int clicked = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_main);


            {
                final TextView text = (TextView) findViewById(R.id.textView2);
                final ImageButton button = (ImageButton)findViewById(R.id.imageButton2);
                button.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        clicked++;
                        text.setText("  " + clicked + " ");

                    }
                });
            }

            final TextView text = (TextView) findViewById(R.id.textView1);
            text.setText("");
            final ImageButton button = (ImageButton)findViewById(R.id.imageButton1);
            button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                clicked++;
                text.setText("  " + clicked + " ");

            }


        });

    }

} 

解决方案

Use two different variables. Clicked is being added to when you press either button. Also choose different names for your buttons, don't name them both button.

ex:

public class MainActivity extends Activity {

int clicked1 = 0;
int clicked2 = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_main);


        {
            final TextView text = (TextView) findViewById(R.id.textView2);
            final ImageButton button2 = (ImageButton)findViewById(R.id.imageButton2);
            button.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    clicked2++;
                    text.setText("  " + clicked2 + " ");

                }
            });
        }

        final TextView text = (TextView) findViewById(R.id.textView1);
        text.setText("");
        final ImageButton button1 = (ImageButton)findViewById(R.id.imageButton1);
        button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            clicked1++;
            text.setText("  " + clicked1 + " ");

        }


    });

}

} 

这篇关于我要创建两个按钮,点击者加起来不同号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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