layout文件:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.hanqi.testapp2.TestActivity4" 11 android:orientation="vertical"> 12 13 <ProgressBar 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" 16 style="?android:attr/progressBarStyleHorizontal" 17 android:progress="0" 18 android:secondaryProgress="0" 19 android:max="80" 20 android:id="@+id/pb_1"/> 21 <ProgressBar 22 android:layout_width="match_parent" 23 android:layout_height="wrap_content" 24 style="?android:attr/progressBarStyleLarge" 25 android:id="@+id/pb_2"/> 26 <SeekBar 27 android:layout_width="match_parent" 28 android:layout_height="wrap_content" 29 android:progress="0" 30 android:max="80" 31 android:secondaryProgress="0" 32 android:id="@+id/se_1"/> 33 <RatingBar 34 android:layout_width="wrap_content" 35 android:layout_height="wrap_content" 36 android:numStars="5" 37 android:rating="3.5" 38 android:isIndicator="true"/> 39 </LinearLayout>
java类代码:
1 package com.hanqi.testapp2; 2 3 import android.app.AlertDialog; 4 import android.os.Bundle; 5 import android.support.v7.app.AppCompatActivity; 6 import android.util.Log; 7 import android.view.View; 8 import android.widget.ProgressBar; 9 import android.widget.SeekBar; 10 11 public class TestActivity4 extends AppCompatActivity { 12 13 SeekBar se_1; 14 ProgressBar pb_1; 15 ProgressBar pb_2; 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity_test4); 20 se_1 = (SeekBar)findViewById(R.id.se_1); 21 pb_1 = (ProgressBar)findViewById(R.id.pb_1); 22 pb_2 = (ProgressBar)findViewById(R.id.pb_2); 23 24 AlertDialog alertDialog = new AlertDialog.Builder(this).create(); 25 26 se_1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 27 //进度变化触发 28 @Override 29 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 30 //设置进度条1的进度值 31 pb_1.setProgress(progress); 32 //判断是否达到最大值 33 if(progress ==se_1.getMax()) 34 { 35 pb_2.setVisibility(View.INVISIBLE);//不显示但位置还保留 36 } 37 else 38 { 39 pb_2.setVisibility(View.VISIBLE); 40 } 41 //只要progress变化就会触发 42 //Toast.makeText(TestActivity4.this, "当前进度 = "+progress, Toast.LENGTH_SHORT).show(); 43 } 44 45 @Override 46 public void onStartTrackingTouch(SeekBar seekBar) { 47 Log.e("TAG","进度条开始拖动"); 48 } 49 50 @Override 51 public void onStopTrackingTouch(SeekBar seekBar) { 52 Log.e("TAG","进度条停止拖动"); 53 } 54 }); 55 } 56 }
效果为: