• ProgressBar与Handler的整合应用


     在android中进度条使用ProgressBar表示,用于向用户显示某个耗时操作完成的百分比     之ProgressBar与Handler的整合应用

    1.在xml文件中通过<ProgressBar>标记添加,表示基本如下:

    1 <ProgressBar        
    2 android:id="@+id/progressBar1"        
    style
    ="?android:attr/progressBarStyleHorizontal"
    android:layout_width
    ="fill_parent"
    android:layout_height
    ="wrap_content" />

    部分xml属性如下:

    android:max-->设置进度条的最大值
    android:progress-->指定进度条已经完成的进度值
    android:progressDrawable-->设置进度条轨道的绘制形式

    关于progressBar的style

    style="?android:attr/progressBarStyleHorizontal"    细长水平进度条
    style="?android:attr/progressBarStyleLarge"         大原型水平进度条

     setIndeterminate  设置indeterminate为false 滚动条告知自己正在忙碌,若为true,则可以根据进度设置进度值

    代码如下

    activity_main.xml
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    
    tools:context=".MainActivity" >    
    <TextView       
     android:id="@+id/tv1"        
    android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" />    
    <ProgressBar       
     android:id="@+id/pb"        
    android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/tv1"        
    android:visibility="gone" />    
    <Button        
    android:id="@+id/btn"       
     android:layout_width="wrap_content"        android:layout_height="wrap_content"        
    android:text="点击"       
     android:layout_below="@id/pb" />
    </RelativeLayout>
    MainActivity.java
    public class MainActivity extends Activity {    
    private TextView tv1;    private ProgressBar pb;    
    private Button btn;    
    private int count;
    @Override    
    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                
    tv1 = (TextView) findViewById(R.id.tv1);        
    pb = (ProgressBar) findViewById(R.id.pb);        
    btn = (Button) findViewById(R.id.btn);        
    //调用setIndeterminate设置indeterminate为false 滚动条告知自己正在忙碌,若为true,则可以根据进度设置进度值        pb.setIndeterminate(false);                btn.setOnClickListener(new View.OnClickListener() {                        
    @Override            
    public void onClick(View v) {                
    //点击按钮,ProgressBar显示                pb.setVisibility(View.VISIBLE);                
    //指定ProgressBar最大值为100                pb.setMax(100);                
    //指定初始值为0                pb.setProgress(0);                                
    //开启线程,更改ProgressBar的值                
    new Thread (new Runnable() {                                                                                
    @Override public void run() { for (int i = 0; i
    < 10; i++) { try { count = (i + 1)*20;
    Thread.sleep(1000); //线程运行5秒后结束 if (i
    == 4) {
    Message msg
    = new Message();
    msg.what
    = 0;
    handle.sendMessage(msg);
    break;
    }else{ Message msg
    = new Message();
    msg.what
    = 1;

    handle.sendMessage(msg);
    } } catch (Exception e) {
    } } } }).start(); } }); } Handler handle
    = new Handler(){ public void handleMessage(Message msg) { switch (msg.what) { case 0: tv1.setText("数据加载完毕"); //设置ProgressBar隐藏
    pb.setVisibility(View.GONE); //线程中断

    Thread.currentThread().interrupt();
    break; case 1: if (!Thread.currentThread().interrupted()) {
    pb.setProgress(count); //将进度显示在setText中
    tv1.setText("
    ==================");

    } break; default: break; } }; };

     下载:https://github.com/SamSarah1/AndroidDemo

    抓住当下,不浮流年!无知本无罪,罪在明知不足,而不肯去学习!!!
  • 相关阅读:
    235. Lowest Common Ancestor of a Binary Search Tree
    112. Path Sum
    111. Minimum Depth of Binary Tree
    110. Balanced Binary Tree
    108. Convert Sorted Array to Binary Search Tree
    win7+iis7.5+asp.net 安装IIS 遇到的一些问题
    创建一个简单的WCF程序
    ROW_NUMBER() OVER函数的基本用法
    input type="file"
    Nightmare
  • 原文地址:https://www.cnblogs.com/SamSarah/p/4888853.html
Copyright © 2020-2023  润新知