Android編程開發(fā)之seekBar采用handler消息處理操作的方法
本文實(shí)例講述了Android編程開發(fā)之seekBar采用handler消息處理操作的方法。分享給大家供大家參考,具體如下:
該案例簡(jiǎn)單實(shí)現(xiàn)進(jìn)度條可走,可拖拽的功能,下面請(qǐng)看源碼:
布局文件:
<RelativeLayout 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: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=".MainActivity" > <SeekBar android:id="@+id/seekBar1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" /> </RelativeLayout>
java文件:
package com.example.lession16_seekbar; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.app.Activity; import android.view.Menu; import android.widget.SeekBar; public class MainActivity extends Activity { private SeekBar seekBar; private static final int MAX_PROGRESS =100; private int current_progress=10; private static final int PRO=1; private Handler handler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); seekBar = (SeekBar) this.findViewById(R.id.seekBar1); seekBar.setMax(MAX_PROGRESS); /* new Thread(new Runnable() { @Override public void run() { while(true){ try { Thread.sleep(600); current_progress+=10; seekBar.setProgress(current_progress); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }).start();*/ handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case PRO: if(current_progress>=MAX_PROGRESS){ }else{ current_progress+=1; seekBar.incrementProgressBy(1); handler.sendEmptyMessageDelayed(PRO, 600); } break; default: break; } } }; current_progress=current_progress>0?current_progress:0; seekBar.setProgress(current_progress); handler.sendEmptyMessage(PRO); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
效果圖(自動(dòng)走):
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android異步消息處理機(jī)制實(shí)現(xiàn)原理詳解
- Android編程實(shí)現(xiàn)異步消息處理機(jī)制的幾種方法總結(jié)
- Android消息處理機(jī)制Looper和Handler詳解
- android的消息處理機(jī)制(圖文+源碼分析)—Looper/Handler/Message
- 關(guān)于Android中Gradle和jar包下載慢的問題及解決方法
- AndroidStudio升級(jí)4.1后啟動(dòng)失敗Plugin問題解決
- AndroidStudio升級(jí)4.1坑(無法啟動(dòng)、插件plugin不好用、代碼不高亮)
- Android Studio 運(yùn)行按鈕灰色的完美解決方法
- 更新至Android Studio4.1后發(fā)現(xiàn)as打不開的解決方法(原因分析)
- 詳解Android 消息處理機(jī)制
相關(guān)文章
Android如何獲取QQ與微信的聊天記錄并保存到數(shù)據(jù)庫詳解
這篇文章主要給大家介紹了關(guān)于Android如何獲取QQ與微信的聊天記錄并保存到數(shù)據(jù)庫的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2018-04-04詳解Flutter 調(diào)用 Android Native 的方法
這篇文章主要介紹了詳解Flutter 調(diào)用 Android Native 的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01Android自定義控件實(shí)現(xiàn)萬能的對(duì)話框
這篇文章主要為大家詳細(xì)介紹了Android自定義控件實(shí)現(xiàn)萬能對(duì)話框的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03Android HttpURLConnection斷點(diǎn)下載(單線程)
這篇文章主要為大家詳細(xì)介紹了Android HttpURLConnection斷點(diǎn)下載的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05Android中用Builder模式自定義Dialog的方法
在任何軟件操作系統(tǒng)中,Dialog即對(duì)話框都是一種重要的交互模式與信息載體,而Android系統(tǒng)本身的Dialog擁有固定的樣式,并且在5.0后采用Material Design設(shè)計(jì)風(fēng)格的Dialog美觀大氣。這篇文章將詳細(xì)介紹Android中用Builder模式自定義Dialog的方法,有需要的可以參考借鑒。2016-10-10Android開發(fā)之設(shè)置開機(jī)自動(dòng)啟動(dòng)的幾種方法
這篇文章主要介紹了Android開發(fā)之設(shè)置開機(jī)自動(dòng)啟動(dòng)的幾種方法的相關(guān)資料,這里提供三種方法幫助大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-08-08android Launcher3設(shè)置默認(rèn)桌面應(yīng)用
這篇文章主要為大家詳細(xì)介紹了android Launcher3設(shè)置默認(rèn)桌面應(yīng)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07