025 Android 帶進度條的對話框(ProgressDialog)
阿新 • • 發佈:2019-05-12
protected final 樣式 ima pat init cte sco pmap
1.ProgressDialog介紹
ProgressDialog可以在當前界面彈出一個置頂於所有界面元素的對話框,同樣具有屏蔽其他控件的交互能力,用於提示用戶當前操作正在運行,讓用戶等待;
2.應用案例
(1)頁面布局的xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".AdvanceToolActivity"> <TextView style="@style/TitleStyle" android:text="高級工具" /> <TextViewandroid:id="@+id/tvAT_query_address" android:text="歸屬地查詢" android:gravity="center" android:textSize="18dp" android:background="@drawable/selector_advanvetool_item_bg" android:drawableLeft="@android:drawable/btn_star" android:layout_width="match_parent"android:layout_height="wrap_content" android:padding="5dp"/> <TextView android:id="@+id/tvAT_sms_copy" android:text="短信備份" android:gravity="center" android:textSize="18dp" android:background="@drawable/selector_advanvetool_item_bg" android:drawableLeft="@android:drawable/btn_star" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp"/> </LinearLayout>
(2)java後臺代碼
package com.example.administrator.test62360safeguard; import android.annotation.SuppressLint; import android.app.ProgressDialog; import android.content.Intent; 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.TextView; public class AdvanceToolActivity extends AppCompatActivity { TextView tvAT_query_address; TextView tvAT_sms_copy; private final static int MAXVALUE = 100; private int currentProgress = 0; private ProgressDialog progressDialog; //更新UI界面 @SuppressLint("HandlerLeak") Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { progressDialog.setProgress(currentProgress); if(currentProgress>=MAXVALUE){ progressDialog.dismiss();//關閉進度條對話框 } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_advance_tool); //電話歸屬地查詢 initPhoneAddress(); //短信備份 initSmsCopy(); } /** * 短信備份 */ private void initSmsCopy() { tvAT_sms_copy=findViewById(R.id.tvAT_sms_copy); tvAT_sms_copy.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showSmsCopyDialog(); } }); } /** * 顯示一個帶進度條的對話框 */ private void showSmsCopyDialog() { progressDialog = new ProgressDialog(this); //註意:這裏的上下文必須是this,而不能用getApplicationContext() progressDialog.setIcon(R.mipmap.ic_launcher); //設置對話框的圖標 progressDialog.setTitle("短信備份"); //設置對話框標題 progressDialog.setMax(MAXVALUE); //設置進度條的最大值 progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); //指定進度條的樣式為水平 progressDialog.show(); //開啟一個線程 new Thread(){ @Override public void run() { while (currentProgress<MAXVALUE){ currentProgress=currentProgress+5; try { Thread.sleep(500); //模擬耗時操作 } catch (InterruptedException e) { e.printStackTrace(); } handler.sendEmptyMessage(0); } } }.start(); } /** * 電話歸屬地查詢 */ private void initPhoneAddress() { tvAT_query_address=findViewById(R.id.tvAT_query_address); tvAT_query_address.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(getApplicationContext(),QueryAddressActivity.class); startActivity(intent); } }); } }
3.效果圖
025 Android 帶進度條的對話框(ProgressDialog)