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
Handler visibilityToggler = new Handler();
Runnable visivilityRunnable = new Runnable() {
@Override
public void run() {
// 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 visibility
Random generator = new Random();
number = generator.nextInt(16);
for (int i = 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(new View.OnClickListener()
{
Button[] s=new Button[]{bt1, bt2, bt3, bt4, bt5, bt6, bt7, bt8,
bt9, bt10, bt11, bt12, bt13, bt14, bt15, bt16};
Random generator = new Random();
int number = generator.nextInt(16);
@Override
public void onClick(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"