How to Create Instance of AsyncTask
I am making a program that when the user clicks the button the asynctask is called in onclick. But everytime the user clicks the button the text changes... How can i implement something that i can call just to utilize the async method.
Here is a example of what i mean
public void Talk(){
text1.setText("Welcome what is your name?");
respond.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
name = edit.getText().toString();
new开发者_如何学Go AsyncTask<Void, Integer, Void>(){
@Override
protected Void doInBackground(Void... arg0) {
try {
Thread.sleep(850);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
text1.setText("Nice to meet you "+name);
dismissDialog(typeBar);
}
@Override
protected void onPreExecute() {
typeBar = 0;
showDialog(typeBar);
}
}.execute((Void)null);
}
});
}
}
So as you can see evertime the user presses the button the text changes. It will definitely be to tedious to type the ayncTask method EVERYTIME the button is clicked. Anyone
Why you don't create an asyntask
class, and you can create an object. but, you should carefully, each object just can use only one time. if you want to use again, you must create other instance
精彩评论