中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

Android 異步處理工具類(AsyncTask)

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用
    <?xml version="1.0" encoding="utf-8"?>  
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"  
        android:orientation="vertical" >  
      
        <ProgressBar  
            android:id="@+id/bar"  
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content"  
            style="?android:attr/progressBarStyleHorizontal"/>  
      
        <TextView  
            android:id="@+id/info"  
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content"  
            android:text="" />  
      
    </LinearLayout>  

.java代碼如下:
import android.app.Activity;  
import android.app.AlertDialog;  
import android.app.Dialog;  
import android.content.DialogInterface;  
import android.os.AsyncTask;  
import android.os.Bundle;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.view.View.OnFocusChangeListener;  
import android.widget.Button;  
import android.widget.EditText;  
import android.widget.ProgressBar;  
import android.widget.TextView;  
  
public class Hello extends Activity {  
    private ProgressBar bar = null;  
    private TextView info = null;  
  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState); // 生命周期方法  
        super.setContentView(R.layout.main); // 設置要使用的布局管理器  
        this.bar = (ProgressBar) super.findViewById(R.id.bar);  
        this.info = (TextView) super.findViewById(R.id.info);  
        ChildUpdate child = new ChildUpdate();//子任務對象  
        child.execute(100);//設置休眠時間  
  
    }  
  
    private class ChildUpdate extends AsyncTask<Integer, Integer, String> {  
//覆寫如下方法  
        @Override  
        protected String doInBackground(Integer... params) {//處理后臺任務  
            for (int x = 0; x < 100; x++) {  
                Hello.this.bar.setProgress(x);//進度條設置  
                this.publishProgress(x);//傳遞每次更新內容  
                try {  
                    Thread.sleep(params[0]);  
                } catch (InterruptedException e) {  
                    e.printStackTrace();  
                }  
  
            }  
            return "執(zhí)行完畢!";  
        }  
  
        @Override  
        protected void onPostExecute(String result) {//任務執(zhí)行完后執(zhí)行  
            Hello.this.info.setText(result);  
        }  
  
        @Override  
        protected void onProgressUpdate(Integer... progress) {//每次更新后的數值  
            Hello.this.info.setText("當前進度為:" + String.valueOf(progress[0]));  
        }  
  
    }  
}  

標簽: 代碼

版權申明:本站文章部分自網絡,如有侵權,請聯系:west999com@outlook.com
特別注意:本站所有轉載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權歸原作者所有,如需使用,請與原作者聯系。

上一篇:使用用Gson操作Json示例

下一篇:繁體和簡體互換的PHP函數