您的位置:首页 > 其它

3.4 复杂进度对话框

2016-02-03 07:36 281 查看
这一小节我们来给大家讲讲复杂进度对话框的用法,先看截图吧



大家一看就知道是一个带有下载进度的对话框,那么了解这个对话框常用用法如下

初始化进度条

设置图标、标题

设置进度条风格

设置进度条起始值

设置进度条每次增长值

这里我们就把ProgressDialog常用的代码方法也列举出来供大家参考

setIcon ,设置进度对话框的图标

setProgress, 设置进度对话框的起始值

setTitle, 设置进度对话框的标题

setProgressStyle, 设置进度对话框的样式

show, 显示对话框

incrementProgressBy,设置进度对话框的增长值

setIndeterminate, 不显示进度值

setMessage, 设置进度对话框中下载内容信息

OnCancelListener,监听进度对话框取消事件

OnDismissListener,监听进度对话框消失事件

下面就是代码的具体实现

<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:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:onClick="show"></Button>

</RelativeLayout>


package com.gxa;

import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnDismissListener;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity implements OnCancelListener, OnDismissListener {
private ProgressDialog dialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

public void show(View view) {
onCreateProgressDialog();
dialog.setOnCancelListener(this);
dialog.setOnDismissListener(this);
}

public void onCreateProgressDialog() {
dialog = new ProgressDialog(this);
dialog.setIcon(R.drawable.ic_launcher);
dialog.setTitle("下载...");
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setProgress(0);
dialog.show();
new Thread(new Runnable() {

@Override
public void run() {
for (int i = 0; i < 25; i++) {
try {
Thread.sleep(1000);
dialog.incrementProgressBy((int) 100 / 25);
} catch (InterruptedException e) {
dialog.cancel();
}
}
dialog.dismiss();
}

}).start();
}

@Override
public void onCancel(DialogInterface dialog) {
Toast.makeText(this, "下载取消", Toast.LENGTH_SHORT).show();
}

@Override
public void onDismiss(DialogInterface dialog) {
Toast.makeText(this, "下载完成", Toast.LENGTH_SHORT).show();
}

}


文字内容来自:国信安刘阳
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: