【发布时间】:2016-02-07 23:18:43
【问题描述】:
我正在尝试实现以下设计/功能:我有一个被圆形进度条包围的圆形图像。我希望当用户触摸 imageButton 时,进度条会像这样开始它的进度:
问题在于,当用户决定将手指从图像按钮 (MotionEvent.ACTION_UP) 移开时,进度条不会停止并返回其初始状态 (0)。
这是我的代码:
public class MainActivity extends AppCompatActivity {
public CircleProgressBar progressBar;
private static final String TAG = MainActivity.class.getSimpleName();
private int totalProgressTime = 100;
Thread t;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
progressBar = (CircleProgressBar)findViewById(R.id.custom_progressBar);
t = new Thread() {
@Override
public void run() {
int jumpTime = 0;
while(jumpTime < totalProgressTime) {
try {
sleep(200);
jumpTime += 5;
final int finalJumpTime = jumpTime;
runOnUiThread(new Runnable() {
@Override
public void run() {
progressBar.setProgress(finalJumpTime);
}
});
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
ImageButton imageButton = (ImageButton)findViewById(R.id.imageButton);
imageButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
if(!t.isAlive()){
t.start();
}else {
t.stop();
}
Toast.makeText(MainActivity.this, "onTouch", Toast.LENGTH_LONG).show();
}
if(event.getAction() == MotionEvent.ACTION_UP){
t.interrupt();
}
Log.d(TAG, "Event type: " + event.toString());
return false;
}
});
}
【问题讨论】:
-
抱歉,我没有完全理解您的需求。进度条在 ACTION_UP 上停止并返回其初始状态是否是有问题的或期望的行为?
-
哦,对不起,我应该更清楚。这是所需的行为。我希望当用户在进度条达到 100 之前执行 ACTION_UP 时自行重置。
标签: android progress-bar