Android控件三(ProgressBar 进度条)

1.直接拖取 将Progressbar拖入虚拟机里面就会在XML里自动生成控件代码

 注:ProgressBar有两个第一个是圆形进度条  第二个是水平进度条(我们今天学习的是水平进度条(Horizontal))


Android控件三(ProgressBar 进度条)

自动生成图中框框区域的内容

   android:progress="0" 是进度条的初始值

   Android控件三(ProgressBar 进度条)[插入]


转到对应的Java类中

根据对应的ID实例化出相应的控件


Android控件三(ProgressBar 进度条)

XML代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:orientation="vertical">


<!--android:progress 初始值-->
<ProgressBar android:id="@+id/progressBar" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:progress="0"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv_main_text"/> <Button android:id="@+id/bu_main_Button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="download" android:text="下载"/> </LinearLayout></LinearLayout>



----------------------------------------------------------------------------------------------------------------------------------------------

Java代码

public class MainActivity extends AppCompatActivity {
   //下载 点击按钮
    private Button bu;
    //文本
    private TextView text;
    //进度条
    private ProgressBar pb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.actitvity_main);
        pb = (ProgressBar) findViewById(R.id.progressBar);
        bu = (Button) findViewById(R.id.bu_main_Button);
        text = (TextView) findViewById(R.id.tv_main_text);
    }


    public  void download(View view){
       new My().start();
    }

    Handler hand=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            int i=msg.what;
            text.setText(i+"%");
        }
    };


    class My extends Thread{
        @Override
        public void run() {
            for (int i = 1; i <= 100; i++) {
                 pb.setProgress(i);
                hand.sendEmptyMessage(i);
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }