MainActivity.java文件
package com.example.jindutiaodemo;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;
public class MainActivity extends Activity {
private ProgressBar progressbar1, progressbar2;
private Handler mhandler;
private int mProgressStatus = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressbar1 = (ProgressBar) findViewById(R.id.progressbar1);
progressbar2 = (ProgressBar) findViewById(R.id.progressbar2);
mhandler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == 0x111) {
progressbar1.setProgress(mProgressStatus);
} else {
Toast.makeText(MainActivity.this, "耗时操作已经完成",
Toast.LENGTH_LONG).show();
progressbar1.setVisibility(View.GONE);
progressbar2.setVisibility(View.GONE);
}
}
};
new Thread(new Runnable() {
public void run() {
while (true) {
mProgressStatus = dowork();
Message m = new Message();
if (mProgressStatus < 100) {
m.what = 0x111;
mhandler.sendMessage(m);
} else {
m.what = 0x110;
mhandler.sendMessage(m);
break;
}
}
}
private int dowork() {
mProgressStatus += Math.random() * 10;
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
return mProgressStatus;
}
}).start();
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ProgressBar
android:id="@+id/progressbar1"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:max="100" />
<ProgressBar
android:id="@+id/progressbar2"
style="@android:style/Widget.ProgressBar.Large"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="25dp"
android:max="100" />
</LinearLayout>
</LinearLayout>
|
|