进度条:
<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"); } }
时间: 2024-11-03 05:44:25