Android UI 之 Progress&ProgressDialog

应用运行时,总有执行耗时的任务的时候,如果这个时候应用不作任何提示就这么干放着,很容易令用户误以为“死机”、“卡了”之类的,然后就做出强制退出,或重启的操作。为了避免用户误会及在此之后作出一些不必要的操作,我们可以使用Progress(进度条)来提示用户等待,应用是正常运行,只是在执行着耗时的任务。

一、Progress
实例
xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.yougel.progressdemo.ProgressActivity">

    <ProgressBar
        android:id="@+id/in_small"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleSmall"/>
    <ProgressBar
        android:id="@+id/in_normal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <ProgressBar
        android:id="@+id/in_large"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleLarge"/>
    <ProgressBar
        android:id="@+id/in_horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:progress="10"
        style="?android:attr/progressBarStyleHorizontal"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="增加进度"
        android:id="@+id/increate"/>
    <Button
        android:id="@+id/discreate"
        android:text="减少进度"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/in_f"
        android:text="变为确定"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/in_t"
        android:text="变为不确定"/>
</LinearLayout>

进度条一般有两种,一种是旋转的进度条,一种是水平的,通过style属性来确定
而水平的又有不确定与确定的区分,通过android:indeterminate属性来确定true表示不确定。
Activity代码:

package com.example.yougel.progressdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

public class ProgressActivity extends AppCompatActivity {

    ProgressBar progressBar,aprogressBar;
    Button btn_ic,btn_dc,btn_in_f,btn_in_t;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_progress);
        init();
    }
    //初始化控件
    public void init(){
        progressBar= (ProgressBar) findViewById(R.id.in_horizontal);
        aprogressBar= (ProgressBar) findViewById(R.id.in_normal);
        btn_ic= (Button) findViewById(R.id.increate);
        btn_dc= (Button) findViewById(R.id.discreate);
        btn_in_f= (Button) findViewById(R.id.in_f);
        btn_in_t= (Button) findViewById(R.id.in_t);
        btn_ic.setOnClickListener(new MyClick());
        btn_dc.setOnClickListener(new MyClick());
        btn_in_f.setOnClickListener(new MyClick());
        btn_in_t.setOnClickListener(new MyClick());
    }

    //自定义点击事件接口
    class MyClick implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            int ID=v.getId();
            switch (ID){
                case R.id.increate:
                    int current=progressBar.getProgress();
                    progressBar.setProgress(current+10);
                    break;
                case R.id.discreate:
                    current=progressBar.getProgress();
                    progressBar.setProgress(current-10);
                    break;
                case R.id.in_f:
                    progressBar.setIndeterminate(false);
                    break;
                case R.id.in_t:
                    progressBar.setIndeterminate(true);
                    break;
            }
        }
    }

    final int LOAD_ID=100;
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuItem loading=menu.add(0,LOAD_ID,0,"loading");
        loading.setActionView(aprogressBar);
        loading.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
        return super.onCreateOptionsMenu(menu);
    }
}

在这里通过getProgress的方法获取当前的进度,当然这是要在确定的进度条才能有效果,然后用setProgress方法设置进度,通过setIndeterminate来设置进度条是确定的还是不确定的

二、ProgressDialog
xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.yougel.progressdemo.PrgressBarActivity">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/in_t"
        android:text="旋转类"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/in_f"
        android:text="水平类"
        />
</LinearLayout>

布局文件添加两个按钮点击生成进度条对话框

Activity代码:

package com.example.yougel.progressdemo;

import android.app.ProgressDialog;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class PrgressBarActivity extends AppCompatActivity {

    Button btn_in_f,btn_in_t;
    ProgressDialog dialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_prgress_bar);
        init();
    }
    public void init() {
        btn_in_f = (Button) findViewById(R.id.in_f);
        btn_in_t = (Button) findViewById(R.id.in_t);
        btn_in_t.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                        dialog=new ProgressDialog(getContext());
                        dialog.setTitle("温馨提示");
                        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                        dialog.setCancelable(false);
                        dialog.setMessage("正在下载");
                        dialog.show();
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                for(int i=5;i>=0;i--){
                                    if(i==0){
                                        dialog.dismiss();
                                    }
                                    final int current=i;
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            dialog.setMessage("窗口会在"+current+"秒后关闭");
                                        }
                                    });
                                    try {
                                        Thread.sleep(1000);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                        }).start();

            }
        });
        btn_in_f.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog=new ProgressDialog(getContext());
                dialog.setTitle("正在下载");
                dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                dialog.setCancelable(false);
                dialog.show();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        for(int i=0;i<=11;i++){
                            if(dialog.getProgress()==100){
                                dialog.dismiss();
                            }
                            final int current=i;
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    dialog.setProgress(current*10);
                                }
                            });
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }).start();
            }
        });
    }
    //


    protected Context getContext(){
        return this;
    }
}

这里的ProgressDialog几个常用的方法
setTitle设置对话框标题
setMessage设置对话框显示的信息
setProgressStyle(ProgressDialog.STYLE_SPINNER)设置为旋转类型的进度条
setProgressStyle(ProgressDialog.STYLE_HORIZONTAL)设置为水平类型的进度条
setCancelable 设置点击对话框外部是否可取消,true表示可以,false表示不可
show显示对话框
在上面的代码中通过新增一个子线程,并在子线程中执行循环操作来修改ProgressDialog的信息,如果不新建子线程直接在主线程操作的话,点击按钮是不会有任何响应的。但子线程又无法对UI进行修改,所以使用了runOnUiThread的方法,把对UI修改的操作放在里面。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值