最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

android實(shí)現(xiàn)簡(jiǎn)單進(jìn)度條ProgressBar效果

 更新時(shí)間:2022年07月20日 10:53:35   作者:愛(ài)吃魚(yú)的貓醬  
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)簡(jiǎn)單進(jìn)度條ProgressBar效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了android實(shí)現(xiàn)簡(jiǎn)單進(jìn)度條ProgressBar的具體代碼,供大家參考,具體內(nèi)容如下

記錄一下今天學(xué)習(xí)的進(jìn)度條ProgressBar

1、在布局文件中添加ProgressBar

<ProgressBar
? ? ? ? android:id="@+id/progressbar"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? style="@android:style/Widget.ProgressBar.Horizontal"
? ? ? ? android:max="100"
? ? ? ? android:layout_marginTop="150dp"
? ? ? ? />

其中**style=“@android:style/Widget.ProgressBar.Horizontal”**設(shè)置進(jìn)度條樣式為水平進(jìn)度條,否則默認(rèn)原型旋轉(zhuǎn)的進(jìn)度條;
max設(shè)置進(jìn)度條長(zhǎng)度,這里設(shè)置為100。

2、java代碼:

聲明ProgressBar;int型的mprogress表示進(jìn)度條進(jìn)度;Handler對(duì)象處理子線程消息

通過(guò)id獲取布局中的ProgressBar;

progressBar=findViewById(R.id.progressbar);

用Handler模擬子線程耗時(shí)操作

mhandler=new Handler(){
? ? ? ? ? ? @Override
? ? ? ? ? ? public void handleMessage(@NonNull Message msg) {
? ? ? ? ? ? ? ? super.handleMessage(msg);
? ? ? ? ? ? ? ? if(msg.what==0x111){
? ? ? ? ? ? ? ? ? ? progressBar.setProgress(mprogress);//進(jìn)度條未完成時(shí),更新進(jìn)度條
? ? ? ? ? ? ? ? }else {
? ? ? ? ? ? ? ? ? ? Toast.makeText(ProgressActivity.this,"耗時(shí)操作完成",Toast.LENGTH_LONG).show();
? ? ? ? ? ? ? ? ? ? progressBar.setVisibility(View.GONE);//設(shè)置進(jìn)度條消失
? ? ? ? ? ?}
? ? ? ?}
? };

1、Handlerd需要重寫handleMessage()方法,參數(shù)為子線程傳來(lái)的msg.
2、msg.what==0x111表示消息是0x111,則進(jìn)度條未完成,此時(shí)需要通過(guò) progressBar.setProgress(mprogress)來(lái)更新進(jìn)度條。
3、msg是其他時(shí)則表示進(jìn)度條完成,這里只彈出Toast,也可以進(jìn)行其他操作。

開(kāi)啟子線程更新UI

new Thread(new Runnable() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void run() {
? ? ? ? ? ? ? ? while (true){
? ? ? ? ? ? ? ? ? ? //進(jìn)度更新
? ? ? ? ? ? ? ? ? ? mprogress=doWork();
? ? ? ? ? ? ? ? ? ? Message message=new Message();
? ? ? ? ? ? ? ? ? ? if(mprogress<100){
? ? ? ? ? ? ? ? ? ? ? ? message.what=0x111;//進(jìn)度條未完成時(shí)的message設(shè)置為0x111
? ? ? ? ? ? ? ? ? ? ? ? mhandler.sendMessage(message);//向handler發(fā)送消息
? ? ? ? ? ? ? ? ? ? }else {
? ? ? ? ? ? ? ? ? ? ? ? message.what=0x110;//進(jìn)度條完成時(shí)的message設(shè)置為0x110
? ? ? ? ? ? ? ? ? ? ? ? mhandler.sendMessage(message);//向handler發(fā)送消息
? ? ? ? ? ? ? ? ? ? ? ? break;//進(jìn)度完成跳出循環(huán)
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }

? ? ? ? ? ? }
? ? ? ? ? ? private int doWork(){
? ? ? ? ? ? ? ? mprogress= (int) (mprogress+Math.random()*10);
? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? Thread.sleep(200);//線程休息200ms
? ? ? ? ? ? ? ? } catch (InterruptedException e) {
? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? return mprogress;
? ? ? ? ? ? }
? ? ? ? }).start();

1、在子線程中通過(guò)doWork()方法模擬耗時(shí)操作
2、doWork()方法改變mprogress的值即可,用來(lái)模擬進(jìn)度條進(jìn)度的值
3、需要一個(gè)Message對(duì)象將消息返回給主線程,
4、當(dāng)mprogress<100即進(jìn)度條未完成時(shí),設(shè)置message.what=0x111->對(duì)應(yīng)當(dāng)進(jìn)度條未完成時(shí),給主線程發(fā)送的message是0x111。
當(dāng)mprogress>100即進(jìn)度條完成時(shí),設(shè)置message.what=0x110->對(duì)應(yīng)當(dāng)進(jìn)度條完成時(shí),給主線程發(fā)送的message是0x110。這里message.what的值可以隨意設(shè)置,不一定非要0x111,0x110。
5、 設(shè)置完message之后需要向主線程發(fā)送message,即mhandler.sendMessage(message);

以上運(yùn)行即可實(shí)現(xiàn)一個(gè)簡(jiǎn)單進(jìn)度條。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

广昌县| 广昌县| 竹山县| 昌都县| 德格县| 宁河县| 旌德县| 韶关市| 昌乐县| 滨州市| 彩票| 泾源县| 阿拉善盟| 吐鲁番市| 洪泽县| 报价| 吉林市| 南平市| 黄梅县| 鹤庆县| 北流市| 玛纳斯县| 礼泉县| 冕宁县| 怀安县| 旌德县| 沂水县| 定边县| 青铜峡市| 达孜县| 江城| 江口县| 庄河市| 铁岭县| 泰宁县| 安陆市| 侯马市| 榆中县| 承德市| 军事| 多伦县|