Reputation: 3
My code is using 'case and break' example
case R.id.student:
showMessage("Student");
Intent std = new Intent(Home.this, student.class);
startActivity(std);
break;
Upvotes: 0
Views: 1079
Reputation: 21531
Using Runnable:
Handler handler = new Handler();
final Runnable r = new Runnable() {
public void run() {
handler.postDelayed(this, 1000);
// TO DO
}
};
Using Handler:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// TO DO
}
}, 1000);
In Your case it is like:
switch (menuButton.getId()) {
case R.id.student:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
showMessage("Student");
Intent std = new Intent(Home.this, student.class);
startActivity(std);
}
}, 1000);
break;
}
Upvotes: 0
Reputation: 16
Let suppose you have clicked a button at second x (startTime) now you need to enable action performed by button on x+2 second and again for next click x+2+2, for that
long startTime = System.currentTimeMillis();
long difference = System.currentTimeMillis() - startTime;
int seconds=difference/1000; //(this will change milisecond to second
required value)
if(seconds>2){
//do task
}else{
return; //do not do task or return
}
// this can be done for one or more than one switches at same time to avaiod multiple click of a button or clicking multiple button at short interval of time
Upvotes: 0
Reputation: 2558
Try this
switch (menuButton.getId()) {
case R.id.student:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
showMessage("Student");
Intent std = new Intent(Home.this, student.class);
startActivity(std);
}
}, 2000);
break;
}
Upvotes: 1
Reputation: 1897
A countdown timer can be used which lies in the object class as follows
new CountDownTimer(2000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
this will start a timer for 2 seconds and you can handle your desired events in the ontick and onfinish block .
you can further refer to android official documentation
Upvotes: 0
Reputation: 104
public void onClick(View view) {
int viewId = view.getId();
if (viewId == R.id.btn_cherry) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// do what you want after 1000 miliseconds
}
}, 1000);
}
Upvotes: 0