java文件写法
package com.example.l.myappli;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextClock;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private int code1=1;//操作进度条
private int code2=2;//操作其他
private ProgressBar pb_main_pro1;
private Button bu_main_but1;
private TextView tv_main_text1;
private int number=0;
private Handler myHandler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int code=msg.what;
if(code==1) {
tv_main_text1.setText(number + "%");
pb_main_pro1.setProgress(number);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bu_main_but1=findViewById(R.id.bu_main_but1);
pb_main_pro1=findViewById(R.id.pb_main_pro1);
tv_main_text1=findViewById(R.id.tv_main_text1);
//设置按钮的点击事件
bu_main_but1.setOnClickListener(this);
}
//点击事件
@Override
public void onClick(View v) {
new MyThread().start();
}
//内部类
//子线程
private class MyThread extends Thread{
@Override
public void run() {
super.run();
while(true){
number++;
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(number==100){
number=0;
break;
}
myHandler.sendEmptyMessage(code1);
}
}
}
}
activity_main.xml配置
1使用LinearLayout布局
2设置属性
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:orientation=“vertical”
android:padding=“20dp”
android:columnCount=“5”
android:rowCount=“10”
3把下面代码放在布局里面
<ProgressBar
android:id="@+id/pb_main_pro1"
style="?android:attr/progressBarStyleHorizontal"
android:progress="0"
android:max="100"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv_main_text1"
android:text="10%"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
<Button
android:id="@+id/bu_main_but1"
android:text="下载按钮"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
实现效果
点击按钮就可以了查看效果了
喜欢的话可以转发,本人对博客的编码方式不太习惯,有不足的地方还请各位可以指出来,我一定改正