應用運行時,總有執行耗時的任務的時候,如果這個時候應用不作任何提示就這麼幹放着,很容易令用戶誤以爲“死機”、“卡了”之類的,然後就做出強制退出,或重啓的操作。爲了避免用戶誤會及在此之後作出一些不必要的操作,我們可以使用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修改的操作放在裏面。