I want to create two button clickers that add up d

2019-09-20 05:22发布

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 + " ");

            }


        });

    }

} 

1条回答
兄弟一词,经得起流年.
2楼-- · 2019-09-20 05:27

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 + " ");

        }


    });

}

} 
查看更多
登录 后发表回答