Android学习-ProgressBar、SeekBar

seekbar手动拖动与progressbar进度绑定

  • 布局代码


     <ProgressBar
         android:id="@+id/progress2"
         style="@style/Widget.AppCompat.ProgressBar.Horizontal"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:padding="20dp"
         android:progress="30" />
    
     <SeekBar
         android:id="@+id/seekbar"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:padding="10dp"/>
    
     <TextView
         android:id="@+id/tv_Progress"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="当前的进度是"
         android:textSize="18dp"/>
    

`

  • 活动代码
public class Test extends AppCompatActivity {
    
    
    private ProgressBar progressBar;
    private SeekBar seekBar;
    private TextView tvprogress;
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        initView();

    }
    public void initView(){
    
    
        progressBar = findViewById(R.id.progress2);
        seekBar = findViewById(R.id.seekbar);
        tvprogress = findViewById(R.id.tv_Progress);

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    
    
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    
    
                //滑杆值发生改变,此方法被触发。1.本身,2.进度栏
                progressBar.setProgress(progress);//滑杆走多少,进度条走多少
                tvprogress.setText("当前进度为" + progress + "%");
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
    
    
                //有touch行为时会触发
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
    
    
                //拖动停止时会触发
                if(progressBar.getProgress() >= progressBar.getMax()){
    
    
                    progressBar.setVisibility(View.INVISIBLE);
                }else{
    
    
                    progressBar.setVisibility(View.VISIBLE);
                }
            }
        });
    }

}

很简单,不知道有用没。。。

猜你喜欢

转载自blog.csdn.net/vbbbbbbbbbbb/article/details/113446692