如何在Android中暂停和恢复圆圈进度条?
public void run(){
new Thread(new Runnable() {
@Override
public void run() {
while (pStatus<100){
pStatus += 1;
mHandler.post(new Runnable() {
@Override
public void run() {
mProgressBar.setProgress(pStatus);
}
});
try {
Thread.sleep(200);
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
}).start();
}
我需要任何圈子进度的例子(可以暂停和恢复)。
答案 0 :(得分:0)
我开发了演示应用程序的源代码如下:
public class MainActivity extends ActionBarActivity {
ProgressBar mProgressBar;
private int pStatus = 0;
boolean resumed = true;
private TextView textView;
Button button1;
private Handler mHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mProgressBar = (ProgressBar) findViewById(R.id.progressBar1);
textView = (TextView) findViewById(R.id.textView1);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (resumed) {
resumed = false;
} else {
resumed = true;
}
}
});
run();
}
public void run() {
new Thread(new Runnable() {
@Override
public void run() {
while (pStatus < 100) {
if (resumed) {
pStatus += 1;
}
mHandler.post(new Runnable() {
@Override
public void run() {
mProgressBar.setProgress(pStatus);
}
});
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
actvity_main.xml文件如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="23dp"
android:layout_marginTop="20dp"
android:indeterminate="false"
android:max="100"
android:minHeight="50dp"
android:minWidth="200dp"
android:progress="1" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/progressBar1"
android:layout_below="@+id/progressBar1" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="52dp"
android:text="Paused" />
</RelativeLayout>
我可以暂停并恢复进度条,您也可以尝试使用此代码。