Alert box goes away on cancel but comes back again to haunt?
All i want to do is display a text for 1000 milliseconds and get the next thing.. dialogs are slow to pop up so i used a textview..but its not working.. the code snip开发者_运维技巧pet is given.. What am I doing wrong?
Thread sleep_for_sometime = new Thread() {
public void run() {
try {
correct.setVisibility(TextView.VISIBLE);
sleep(1000);
correct.setVisibility(TextView.INVISIBLE);
} catch (InterruptedException e) {
e.printStackTrace();
}
finally {
get_question(questionset[i]);
}
}
};
//sleep_for_sometime.setDaemon(false);
sleep_for_sometime.start();
You should change visibility in UI thread. Read painless threading
AsyncTask
is great, but overused in some ways I think. Just use post
/postDelayed
. You're not really doing background work, you just want to delay some UI-thread work. Definitely read Nikita's link, but I would do something like:
correct.setVisibility(View.VISIBLE);
correct.postDelayed(new Runnable() {
correct.setVisibility(View.INVISIBLE);
get_question(questionset[i]);
}, 1000);
Expanding Nikita's Answer , you could do something like this
private class SleepTask extends AsyncTask<Void,Integer,Void> {
@Override
protected Void doInBackground(Void... params) {
try {
publishProgress(0);
sleep(1000);
publishProgress(1);
}catch(Exception e){
//null
}
}
@Override
protected void onProgressUpdate(Integer... i){
if(i[0]==0){
correct.setVisibility(TextView.VISIBLE);
}else{
correct.setVisibility(TextView.INVISIBLE);
}
}
@Override
protected void onPostExecute(Void res) {
get_question(questionset[i]);
}
@Override
protected void onPreExecute() {
//do something before execution
}
}
to start the thread , do this
new SleepTask().execute();
精彩评论