ProgressBar

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++;
    }
}

效果前:
这里写图片描述
效果后:
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值