Reputation: 145
I try to do app which show elements. Each element should start showing when the before element was hidden. Each element is showing 2 seconds. Code:
public void gameStart()
{
do
{
data = random.nextInt(6) + 1;
if (data == 1)
{
CountDownTimer cdt = new CountDownTimer(2000, 1000)
{
@Override
public void onTick(long millisUntilFinished)
{
element1.setVisibility(View.VISIBLE);
}
@Override
public void onFinish()
{
element1.setVisibility(View.GONE);
}
}.start();
{
CountDownTimer cdt = new CountDownTimer(2000, 1000)
{
@Override
public void onTick(long millisUntilFinished)
{
element2.setVisibility(View.VISIBLE);
}
@Override
public void onFinish()
{
element2.setVisibility(View.GONE);
}
}.start();
} else if (data == 3)
{
CountDownTimer cdt = new CountDownTimer(2000, 1000)
{
@Override
public void onTick(long millisUntilFinished)
{
element3.setVisibility(View.VISIBLE);
}
@Override
public void onFinish()
{
element3.setVisibility(View.GONE);
}
}.start();
} else if (data == 4)
{
CountDownTimer cdt = new CountDownTimer(2000, 1000)
{
@Override
public void onTick(long millisUntilFinished)
{
element4.setVisibility(View.VISIBLE);
}
@Override
public void onFinish()
{
element4.setVisibility(View.GONE);
}
}.start();
} else if (data == 5)
{
CountDownTimer cdt = new CountDownTimer(2000, 1000)
{
@Override
public void onTick(long millisUntilFinished)
{
element5.setVisibility(View.VISIBLE);
}
@Override
public void onFinish()
{
element5.setVisibility(View.GONE);
}
}.start();
} else if (data == 6)
{
CountDownTimer cdt = new CountDownTimer(2000, 1000)
{
@Override
public void onTick(long millisUntilFinished)
{
element6.setVisibility(View.VISIBLE);
}
@Override
public void onFinish()
{
element6.setVisibility(View.GONE);
}
}.start();
}
id = id + 1;
text.setText("cos " + id);
} while (id < 3);
All elements are being shown in the same time. And I try add
Thread.sleep(2500);
But this stoped the action in window. I try add this after code:
Timer timer = new Timer();
timer.schedule( new TimerTask() {
public void run() {
}
}, 0, 60*1000);
But loop wasn't stay. I try do it with notify()
and wait()
, but it also don't work.
Don't suggest me to do do next action in onFinish()
because this must be repeat a lot.
Anybody have another idea?
EDIT I also use Handler but it don't work
Upvotes: 2
Views: 1410
Reputation: 2604
I would recommend you not to implement this code in the main thread. Create an async task, which hides a button and on postExequte calls itself.
Upvotes: 1