android实践项目八做一个下载读条

用progressBar做一个下载读条

效果如下
布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.httpclientexample.MainActivity" >


    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="start"
        android:text="start" />

  <ProgressBar
      android:id="@+id/progressBar1"
      style="?android:attr/progressBarStyleHorizontal"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" />
<TextView 
    android:id="@+id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />
</LinearLayout>

代码

public class MainActivity extends Activity {
    private ProgressBar progress;
    private TextView text;
    private Button btn;
    int Processvalue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = (TextView) findViewById(R.id.text1);
        progress = (ProgressBar) findViewById(R.id.progressBar1);
        progress.setVisibility(View.INVISIBLE);
    }

    public void start(View view) {
        uzi();
        getView();
    }

    private void getView() {
        // TODO Auto-generated method stub

    }

    private void uzi() {
        // TODO Auto-generated method stub
        // 第三个参数是结果类型
        new AsyncTask<String, Integer, String>() {
            // 此方法必须重写,异步执行后台线程需要完成的任务

            @Override
            protected String doInBackground(String... params) {
                for (int i = 0; i <= 100; i++) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    Processvalue = i;
                    publishProgress(Processvalue);
                }
                return null;
            }

            // 执行后台耗时操作时前被调用,通常用来完成一些初始化操作
            @Override
            protected void onPreExecute() {

                super.onPreExecute();
                progress.setVisibility(View.VISIBLE);
                progress.setMax(100);
                progress.setProgress(0);
            }

            // 当doinBackground()完成过后,系统自动调用,并将方法的返
            // 回值传给onpostexecute()可运行在主线程操作ui
            @Override
            protected void onPostExecute(String result) {
                // TODO Auto-generated method stub

                super.onPostExecute(result);
                progress.setVisibility(View.INVISIBLE);
            }

            // doinBackground()方法中调用publishProgress()方法更行任务的执行进度后,就会触发该方法
            @Override
            protected void onProgressUpdate(Integer... values) {
                // TODO Auto-generated method stub
                super.onProgressUpdate(values);
                int value = values[0];
                text.setText("当前进度" + value + "%");
                progress.setProgress(value);
            }

            @Override
            protected void onCancelled(String result) {
                // TODO Auto-generated method stub
                super.onCancelled(result);
            }

            @Override
            protected void onCancelled() {
                // TODO Auto-generated method stub
                super.onCancelled();
            }

        }.execute();
    }
}

用AsyncTask实现效果。。。。

posted @ 2016-04-24 13:44  Tesi1a  阅读(110)  评论(0编辑  收藏  举报