《AsyncTask子类.class文件》 import android.os.AsyncTask; import android.util.Log; import android.widget.Button; import android.widget.EditText; /** * AsyncTask使用方法 * 1--自定义AsyncTask * 继承 AsyncTask<Params, Progress, Result> * Params:调用类中执行execute()方法时传入的参数。 * 如果不需要传递参数,则为Void。 * Progress:子线程执行的进度。 * Result: 和doInBackground()方法的返回值类型保持一致。 * 2--调用自定义的AsyncTask * 在调用类中生成一个自定义AsyncTask对象(通常实例化自定义AsyncTask的子类), * 然后执行:对象.excute(Params... params); */ public class PrintDynamic extends AsyncTask<Void,Integer,Void> { EditText ed; Button bt; //构造函数,实例化后可以调用该类的函数方法并执行 public PrintDynamic(EditText editText,Button button){ this.ed=editText; this.bt=button; } private static final String TAG="Async"; @Override protected Void doInBackground(Void... voids) { //该方法中是后台执行的内容,比如睡眠,下载,播放歌曲等 for(int i=0;i<50;i++){ try { Thread.sleep(1000);//线程睡眠需要抓取错误 } catch (InterruptedException e) { e.printStackTrace(); } publishProgress(i);//i就是能够传递给onProgressUpdate的参数values } Log.d(TAG, "doInBackground: "); return null; } @Override protected void onProgressUpdate(Integer... values) {//values打印线程中的参数数组,线程有变化就执行 if(ed!=null){ ed.setText(values[0].toString()); } Log.d(TAG, "onProgressUpdate: "); } @Override protected void onPostExecute(Void aVoid) {//当onProgressUpdate后台执行的方法完成后执行一次 if(bt!=null){ bt.setText("完成"); } } } 《主页面Activity》import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Button; import android.widget.EditText; public class MYActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); } @Override protected void onResume() { super.onResume(); Button button= (Button) findViewById(R.id.button); EditText editText= (EditText) findViewById(R.id.edname); new PrintDynamic(editText,button).execute();//实例化并执行 } }
《layout布局.xml文件》<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/name" android:layout_width="100dp" android:layout_height="50dp" android:textColor="@color/colorAccent" android:gravity="center" android:textSize="20dp" android:text="用户名" /> <EditText android:id="@+id/edname" android:layout_width="180dp" android:layout_height="50dp" android:hint="请输入用户名" android:text="huyue" android:layout_toRightOf="@id/name" android:textColor="@color/colorPrimary" /> </RelativeLayout> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/password" android:layout_width="100dp" android:layout_height="50dp" android:gravity="center" android:textSize="19dp" android:textColor="@color/colorAccent" android:text="密码" /> <EditText android:layout_width="180dp" android:layout_height="50dp" android:hint="输入密码" android:layout_toRightOf="@id/password" /> </RelativeLayout> <Button android:id="@+id/button" android:layout_width="96dp" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_marginRight="105dp" android:text="登录" /> </LinearLayout>
AsyncTash基础创建规则与例子
最新推荐文章于 2022-08-25 11:13:19 发布