欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android ProgressBar直線進(jìn)度條的實(shí)例代碼

 更新時(shí)間:2017年06月08日 10:19:32   作者:DistanceZK  
本文通過(guò)實(shí)例代碼給大家介紹了android progressbar直線進(jìn)度條的實(shí)現(xiàn)方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧

直線進(jìn)度條效果圖:

點(diǎn)擊下載后的效果圖:

布局xml文件:

empty 

Java代碼:

package com.example.android_rogressbar;
import android.os.Handler;
import android.os.Message;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
  private ProgressBar pb_progress_bar;
  private TextView tv_main_text;
  private ImageView iv_main_image;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //根據(jù)ID獲取控件
    pb_progress_bar = (ProgressBar) findViewById(R.id.pb_progress_bar);
    tv_main_text = (TextView) findViewById(R.id.tv_main_text);
  }
  //下載的方法
  public void download(View view){
    //啟動(dòng)線程
    new MyThread().start();
  }
  Handler handler=new Handler(){
    //接收消息,用于更新UI界面
    @Override
    public void handleMessage(Message msg) {
      super.handleMessage(msg);
      int i=msg.what;
      tv_main_text.setText(i+"");
    }
  };
  class MyThread extends Thread{
    @Override
    public void run() {
      super.run();
      for (int i = 0; i <= 100; i++) {
        pb_progress_bar.setProgress(i);
        //在子線程中發(fā)送消息
        handler.sendEmptyMessage(i);
        try {
          Thread.sleep(100);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    }
  }
}

ProgressBar.java

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:orientation="vertical"
  xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.example.android_rogressbar.MainActivity">
  <!--style:設(shè)置進(jìn)度條的樣式,這里為直線進(jìn)度條-->
  <ProgressBar
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/pb_progress_bar"
    />
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tv_main_text"
    />
  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="下載"
    android:onClick="download"
    />
</LinearLayout>

因?yàn)橹骶€程執(zhí)行耗時(shí)代碼會(huì)報(bào)錯(cuò),所以我們新建一個(gè)子線程來(lái)執(zhí)行進(jìn)度條

在子程序中我們沒(méi)辦法對(duì)控件進(jìn)行操作,所以我們需要用到handler類(lèi),實(shí)現(xiàn)主線程和子線程之間的通信;

Handler的定義

主要接受子線程發(fā)送的數(shù)據(jù),并用此數(shù)據(jù)配合主線程更新UI。

當(dāng)應(yīng)用程序啟動(dòng)時(shí),Android首先會(huì)開(kāi)啟一個(gè)主線程 (即UI線程),主線程管理界面中的UI控件,進(jìn)行事件分發(fā),比如說(shuō):點(diǎn)擊Button,Android系統(tǒng)會(huì)分發(fā)事件到Button上,來(lái)響應(yīng)你的操作。如果此時(shí)需要一個(gè)耗時(shí)的操作,例如: 聯(lián)網(wǎng)讀取數(shù)據(jù),或者讀取本地較大的一個(gè)文件的時(shí)候,你不能把這些操作放在主線程中,如果你放在主線程中的話,界面會(huì)出現(xiàn)假死現(xiàn)象,如果5秒鐘還沒(méi)有完成的話,會(huì)收到Android系統(tǒng)的一個(gè)錯(cuò)誤提示“強(qiáng)制關(guān)閉”。這個(gè)時(shí)候我們需要把這些耗時(shí)的操作,放在一個(gè)子線程中。
因?yàn)樽泳€程涉及到UI更新,Android主線程是線程不安全的,也就是說(shuō),更新UI只能在主線程中更新,子線程中操作是危險(xiǎn)的。這個(gè)時(shí)候,Handler就出現(xiàn)了。來(lái)解決這個(gè)復(fù)雜的問(wèn)題,由于Handler運(yùn)行在主線程中(UI線程中), 它與子線程可以通過(guò)Message對(duì)象來(lái)傳遞數(shù)據(jù),這個(gè)時(shí)候,Handler就承擔(dān)著接受子線程傳過(guò)來(lái)的(子線程用sedMessage()方法傳遞Message對(duì)象,(里面包含數(shù)據(jù)),把這些消息放入主線程隊(duì)列中,配合主線程進(jìn)行更新UI。

以上所述是小編給大家介紹的Android ProgressBar直線進(jìn)度條的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論