如何创建AsyncTask的实例

时间:2011-06-21 00:01:38

标签: android android-asynctask

我正在创建一个程序,当用户单击该按钮时,onclick中会调用asynctask。但每次用户点击按钮时文本都会发生变化......我怎么能实现一些我可以调用的东西来利用异步方法。

这是我的意思的一个例子

        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 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);

        }
    });






}
    }

因此,当您看到evertime时,用户按下按钮,文本会发生变化。单击按钮,输入ayncTask方法非常繁琐。任何人

1 个答案:

答案 0 :(得分:0)

为什么不创建asyntask类,可以创建对象。但是,你应该小心,每个对象只能使用一次。如果要再次使用,则必须创建其他实例