0
点赞
收藏
分享

微信扫一扫

Android中通过SeekBar手动控制ProgressBar与模拟下载自动更新进度条


场景

进度条的常用场景:

通过SeekBar拖动进而更新ProgressBar进度条,比如调整音量效果等。

模拟下载实现自动更新进度条。

注:

实现

手动控制进度条

在布局文件xml中添加一个SeekBar与ProgressBar

<ProgressBar
android:id="@+id/pb_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"/>

<SeekBar
android:id="@+id/seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"/>

然后在Activity中声明这两个控件

private ProgressBar  progressBar;
private SeekBar seekBar;

然后在onCreate方法中获取这两个控件

progressBar = findViewById(R.id.pb_test);
seekBar = findViewById(R.id.seekbar);

并设置seeBar的进度改变的监听器以及重写其方法

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
progressBar.setProgress(progress);
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {

}
});

onProgressChanged方法就是其进度改变的事件监听,将进度条的进度设置为seekBar的进度

实现效果

 

Android中通过SeekBar手动控制ProgressBar与模拟下载自动更新进度条_ide

自动进度条实现

在布局文件添加一个TextView用来显示数字显示的百分比,添加一个Button用来触发下载操作

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ProgressBarActivity">

<TextView
android:id="@+id/tv_persent"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ProgressBar
android:id="@+id/pb_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"/>

<SeekBar
android:id="@+id/seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"/>

<Button
android:id="@+id/btn_download"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="下载"/>
</LinearLayout>

然后在Activity中,在Button的点击事件中

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
handler.sendEmptyMessage(1);
}
});
thread.start();
}
});

新开一个进程并启动,新开的进程与主进程进行通信使用新建的Handler对象,调用其sendEmptyMessage方法

发送一个空消息参数为一个int值,这里为1。

然后在Handler初始化时重写其handlerMessage方法

使其每延迟200毫秒加一个进度并更新TextView和ProgressBar的进度显示。

完整的activity的示例代码

package com.badao.androidstudy;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

public class ProgressBarActivity extends AppCompatActivity {

private ProgressBar progressBar;
private SeekBar seekBar;
private Button button;
private TextView textView;
private Handler handler;
private int progress = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_progress_bar);

progressBar = findViewById(R.id.pb_test);
seekBar = findViewById(R.id.seekbar);
button = findViewById(R.id.btn_download);
textView = findViewById(R.id.tv_persent);

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
progressBar.setProgress(progress);
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {

}
});
handler = new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 1:
if(progress<100)
{
progressBar.setProgress(progress);
textView.setText(progress+"%");
progress++;
handler.sendEmptyMessageDelayed(1,200);
}else
{
Toast.makeText(ProgressBarActivity.this,"下载完成",Toast.LENGTH_LONG).show();
}
break;
}
}
};
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
handler.sendEmptyMessage(1);
}
});
thread.start();
}
});
}
}

示例效果

Android中通过SeekBar手动控制ProgressBar与模拟下载自动更新进度条_android_02

 

 

 

举报

相关推荐

0 条评论