標籤:
進度條:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ProgressBar 預設進度條 android:id="@+id/pb1" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <ProgressBar android:id="@+id/pb2" android:layout_width="match_parent" android:layout_height="wrap_content" style="@android:style/Widget.ProgressBar.Horizontal" android:max="100" 最大值是100 android:progress="30"/> 初始值是30 <Button android:id="@+id/btnDownload" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Download File"/></LinearLayout>
package com.sxt.day03_07;import android.app.Activity;import android.os.Bundle;import android.os.SystemClock;import android.view.View;import android.view.View.OnClickListener;import android.widget.ProgressBar;public class MainActivity extends Activity { ProgressBar mProgressBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); setListener(); } private void setListener() { findViewById(R.id.btnDownload).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { new Thread(){//開出一個線程,則進度條工作的時候,不影響主線程的工作,如果不用多線程則進度條工作的時候再點擊別的地方就會有問題 public void run() { for(int i=1;i<=100;i++){ mProgressBar.setProgress(i); SystemClock.sleep(20);//SystemClock.sleep(20)是安卓裡的暫停,進度條變化一次使當前線程休眠10毫秒 } }; }.start(); } }); } private void initView() { mProgressBar=(ProgressBar) findViewById(R.id.pb2); mProgressBar.setProgress(0); }}
拖動條:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <SeekBar android:id="@+id/seekBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progress="50"/></RelativeLayout>
package com.sxt.day03_08;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.widget.SeekBar;import android.widget.SeekBar.OnSeekBarChangeListener;public class MainActivity extends Activity implements OnSeekBarChangeListener{ int mProgress;//代表SeekBar當前的progress值 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); SeekBar seekBar=(SeekBar) findViewById(R.id.seekBar); seekBar.setOnSeekBarChangeListener(this);//給SeekBar添加拖動監聽器 } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {//拖動中一直調用這個函數,fromUser為true表示潤許觸摸操作,progress表示當前拖動值, mProgress=progress; Log.i("main","progress="+progress); } @Override public void onStartTrackingTouch(SeekBar seekBar) {//開始拖動函數 Log.i("main","開始拖動seekbar"); } @Override public void onStopTrackingTouch(SeekBar seekBar) {//結束拖動函數 Log.i("main","結束拖動seekbar"); }}
android 14 進度條和拖動條