activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn" />
<ProgressBar
android:id="@+id/progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"/>
<Button
android:id="@+id/btnprogressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnprogressbar" />
<!--小 -->
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@android:style/Widget.ProgressBar.Small"/>
<!--中 -->
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@android:style/Widget.ProgressBar.Inverse"/>
<!--大 -->
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@android:style/Widget.ProgressBar.Large"/>
<!--水平进度条 -->
<ProgressBar
android:id="@+id/pd2"
android:max="100"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@android:style/Widget.ProgressBar.Horizontal"/>
</LinearLayout>
MainActivity.java
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button btn;
private ProgressBar pbl;
private Button btnprogressBar;
//水平进度条
private ProgressBar pbl2;
//水平进度条的最大值
private int max=100;
//水平进度条默认从0
int currentProgress=0;
Handler myhandler=new Handler(){
public void handleMessage(Message msg) {
if (msg.what==0) {
pbl.setVisibility(View.VISIBLE);
}else if (msg.what==1){
pbl.setVisibility(View.GONE);
Toast.makeText(MainActivity.this, "加载成功", Toast.LENGTH_SHORT).show();
}else if (msg.what==2){//更新水平进度条
pbl2.setProgress(currentProgress);
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button) findViewById(R.id.btn);
btn.setOnClickListener(new MyOnClickListener());
pbl=(ProgressBar) findViewById(R.id.progressbar);
btnprogressBar=(Button) findViewById(R.id.btnprogressbar);
btnprogressBar.setOnClickListener(new MyOnClickListenerImpl());
//水平进度条
pbl2=(ProgressBar) findViewById(R.id.pd2);
new Thread(){
@Override
public void run() {
while (currentProgress<max) {
loadData2();
myhandler.sendEmptyMessage(2);
}
}
}.start();
}
/**
* 加载提示
* @author Administrator
*
*/
private class MyOnClickListener implements OnClickListener{
@Override
public void onClick(View v) {
Toast myToast=Toast.makeText(MainActivity.this, "自定义", Toast.LENGTH_LONG);
//获取视图
LinearLayout toastview=(LinearLayout) myToast.getView();
ImageView img=new ImageView(MainActivity.this);
img.setImageResource(R.drawable.ic_launcher);//添加图片
toastview.addView(img,0);//将图片添加到视图中 0图片在汉字上面 1图片在汉字下面
myToast.show();
}
}
private class MyOnClickListenerImpl implements OnClickListener{
@Override
public void onClick(View v) {
myhandler.sendEmptyMessage(0);
new Thread(){
@Override
public void run() {
loadData();
myhandler.sendEmptyMessage(1);
}
}.start();
}
}
/**
* 加载数据
*/
private void loadData(){
try {
Thread.sleep(5000);//休息时间
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private int loadData2(){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return currentProgress++;
}
}
效果前:
效果后: