Skip to content Skip to sidebar Skip to footer

Make Button Method If Button Clicked Or Not Cliked

in here i had a lot of button that randomly turn to visible bt1 = (Button)findViewById(R.id.yellow1); bt2 = (Button)findViewById(R.id.yellow2); bt3 = (Button)findVi

Solution 1:

Check out this

HandlervisibilityToggler=newHandler();


        RunnablevisivilityRunnable=newRunnable() {
            @Overridepublicvoidrun() {

                 // isUserClickedButton is used to keep record if user has pressed button within 1 sec//  keep isUserClickedButton = true for first time as it will run if (!isUserClickedButton) {

                    // user not pressed button
                    Toast.makeText(context,"You are not pressed the Button",Toast.LENGHT_LONG).show();
                }

                // toggle visibilityRandomgenerator=newRandom();
                number = generator.nextInt(16);

                for (inti=0; i < buttons.length; i++) {
                    if (i == number)
                        buttons[i].setVisibility(View.VISIBLE);
                    else
                        buttons[i].setVisibility(View.INVISIBLE);
                }

            // again start the visibility 
              visibilityToggler.postDelayed(visivilityRunnable,1000);

                // make it false as visibility is toggled and we want to track button pressed from start
                isUserClickedButton = false;


            }
        };

visibilityToggler.postDelayed(visivilityRunnable,1000);

Onclick handling if user pressed button

if (click == bt1 || click == bt2 || click == bt3 || click == bt4 || click == bt5 || click == bt6 || click == bt7 || click == bt8 ||
                click == bt9 || click == bt10 || click == bt11 || click == bt12 || click == bt13 || click == bt14 || click == bt15 || click == bt16) {

            //will do something// make it true as user is pressed button and we don't want to run condition of not pressed after 1 sec
            isUserClickedButton = true;


        }
    }

Solution 2:

b1.setOnClickListener(newView.OnClickListener()

    {
        Button[] s=newButton[]{bt1, bt2, bt3, bt4, bt5, bt6, bt7, bt8, 
                                     bt9, bt10, bt11, bt12, bt13, bt14, bt15, bt16};
        Randomgenerator=newRandom();
        intnumber= generator.nextInt(16);

        @OverridepublicvoidonClick(View v) {

            for( int i=0; i<s.length; i++ )
            {
                if( i == number )

                    s[i].setVisibility(View.VISIBLE);
                else

                    s[i].setVisibility(View.INVISIBLE);

            }
        }
    });

Solution 3:

you can define a Boolean value and use onclicklistener for your button, then if user clicks the button the Boolean value will be false and he cannot write code. It's a flag.

Post a Comment for "Make Button Method If Button Clicked Or Not Cliked"