Android常用控件七之ProgressBar的代码用法
2017-06-07 19:37
417 查看
效果图:
布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下载"
android:onClick="ok"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv"/>
</LinearLayout>
Java代码:
package com.zking.laci.android07;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
/**
* Created by Laci on 2017/6/7.
*/
public class ProgressActivity extends AppCompatActivity{
private ProgressBar pb;
private TextView tv;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_progressbar);
//通过ID拿到相对应的控件
pb = (ProgressBar) findViewById(R.id.progressBar);
tv = (TextView) findViewById(R.id.tv);
}
public void ok(View v){
new MyThread().start();
}
//接收消息,更新UI界面
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int i=msg.what;
//给文本框设置值
tv.setText(i+"");
}
};
class MyThread extends Thread{
@Override
public void run() {
super.run();
for (int i = 0; i <=100 ; i++) {
//控制进度条的走动
pb.setProgress(i);
//在子线程中发消息
handler.sendEmptyMessage(i);
try {
//每0.1秒停止
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下载"
android:onClick="ok"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv"/>
</LinearLayout>
Java代码:
package com.zking.laci.android07;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
/**
* Created by Laci on 2017/6/7.
*/
public class ProgressActivity extends AppCompatActivity{
private ProgressBar pb;
private TextView tv;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_progressbar);
//通过ID拿到相对应的控件
pb = (ProgressBar) findViewById(R.id.progressBar);
tv = (TextView) findViewById(R.id.tv);
}
public void ok(View v){
new MyThread().start();
}
//接收消息,更新UI界面
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int i=msg.what;
//给文本框设置值
tv.setText(i+"");
}
};
class MyThread extends Thread{
@Override
public void run() {
super.run();
for (int i = 0; i <=100 ; i++) {
//控制进度条的走动
pb.setProgress(i);
//在子线程中发消息
handler.sendEmptyMessage(i);
try {
//每0.1秒停止
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
相关文章推荐
- Android开发--常用控件 ProgressBar
- Android 常用控件自定义样式RadioButton、CheckBox、ProgressBar、
- Android控件之ProgressBar基本用法
- Android常用控件之GridView与ExpandableListView的用法
- Android笔记---常用控件以及用法
- Android中的常用控件及其基本用法
- Android常用控件之GridView与ExpandableListView的用法
- Android常用控件之进度条(Progressbar)
- Android控件之ProgressBar用法实例分析
- Android开发之常用框架WebView详解代码。超详细,送给初学者,完全掌握此控件
- Android常用控件(三)- ProgressBar、ListView
- Android 常用控件自定义样式RadioButton、CheckBox、ProgressBar、
- Android中的常用控件及其基本用法
- Android(六) 常用控件(二)单选框,toast,checkbox,progressbar,listview
- android常用控件(三)- ProgressBar、ListView
- Android中的常用控件及其基本用法
- android 常用控件之 滚动条 (ProgressBar)
- 【读书笔记《Android游戏编程之从零开始》】5.Android 游戏开发常用的系统控件(ProgressBar、Seekbar)
- Android中的常用控件及其基本用法
- Android常用控件之ProgressBar系列