Skip to content Skip to sidebar Skip to footer

I Want To Create Two Button Clickers That Add Up Different Numbers

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 anyt

Solution 1:

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:

publicclassMainActivityextendsActivity {

intclicked1=0;
intclicked2=0;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stubsuper.onCreate(savedInstanceState);
    setContentView(R.layout.layout_main);


        {
            finalTextViewtext= (TextView) findViewById(R.id.textView2);
            finalImageButtonbutton2= (ImageButton)findViewById(R.id.imageButton2);
            button.setOnClickListener(newView.OnClickListener() {

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

                }
            });
        }

        finalTextViewtext= (TextView) findViewById(R.id.textView1);
        text.setText("");
        finalImageButtonbutton1= (ImageButton)findViewById(R.id.imageButton1);
        button.setOnClickListener(newView.OnClickListener() {

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

        }


    });

}

} 

Post a Comment for "I Want To Create Two Button Clickers That Add Up Different Numbers"