AlertDialog自定义下载进度条

AlertDialog自定义下载进度条

1.布局文件

  1. activity_main.xml
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    <pre class="prettyprint lang-js linenums"><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"
    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=".MainActivity" >
    <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="下载" />
    </RelativeLayout></pre>
  2. two_activity.xml(自定义对话框内部显示的控件)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    <pre class="prettyprint lang-js linenums"><?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="60dp"
    android:text="文件已下载"
    android:textAppearance="?android:attr/textAppearanceLarge" />

    <ProgressBar
    android:id="@+id/progressBar1"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="81dp" />

    </RelativeLayout></pre>
  3. MainActivity.java文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.example.test;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
/***
* 自定义下载进度对话框
* @author hdcking.cn
*/
public class MainActivity extends Activity {
Button btn = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
// builder.setTitle("下载进度");
// builder.setIcon(R.drawable.ic_launcher);
// LayoutInflater inflater = getLayoutInflater();
// builder.setView(inflater.inflate(R.layout.two_activity, null));
// builder.show();
ProgressDialog pd = new ProgressDialog(MainActivity.this);
pd.setTitle("文件下载");
pd.setIcon(R.drawable.ic_launcher);
pd.setMax(100);//文件最大加载数
pd.setMessage("文件已下载");
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//进度条风格
pd.setCancelable(true);//点击返回是否退出
pd.show();
}
});
}
}

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!