Android控件之ProgressDialog

水平进度条

代码示例

	<span style="font-size:18px;">private void setHorDialog() {
		// TODO Auto-generated method stub
		final ProgressDialog dialog = new ProgressDialog(this);
		// 设置为水平的样式
		dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		// 设置可以通过点击Back键取消
		dialog.setCancelable(true);
		// 设置点击进度条窗体外不可取消
		dialog.setCanceledOnTouchOutside(false);
		// 设置Title图标
		dialog.setIcon(R.drawable.ic_launcher);

		dialog.setTitle("提示");
		dialog.setMax(100);
		dialog.setMessage("这是一个水平的进度条");
		dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定",
				new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						Toast.makeText(MainActivity.this, "BUTTON_POSITIVE",
								Toast.LENGTH_SHORT).show();
					}
				});
		dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",
				new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						Toast.makeText(MainActivity.this, "BUTTON_NEGATIVE",
								Toast.LENGTH_SHORT).show();
					}
				});
		dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "中立",
				new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						Toast.makeText(MainActivity.this, "BUTTON_POSITIVE",
								Toast.LENGTH_SHORT).show();
					}
				});
		dialog.show();
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				int i=0;
				while(i<100){
					try {
						Thread.sleep(200);
						//每次加1
						dialog.incrementProgressBy(1);
						//二级进度条更新方式  
					    dialog.incrementSecondaryProgressBy(10);
						i++;
					} catch (Exception e) {
						// TODO: handle exception
					}					
				}
				dialog.dismiss();
			}
		}).start();
	}</span>

效果示意


圆形进度条

代码示例

<span style="font-size:24px;">	</span><span style="font-size:18px;">private void setCirDialog() {
		// TODO Auto-generated method stub
		final ProgressDialog dialog = new ProgressDialog(this);
		// 设置进度条的形式为圆形转动的进度条
		dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		// 设置是否可以通过点击Back键取消
		dialog.setCancelable(true);
		// 设置在点击Dialog外是否取消Dialog进度条
		dialog.setCanceledOnTouchOutside(false);
		
		dialog.setIcon(R.drawable.ic_launcher);
		dialog.setTitle("提示");
		// dismiss监听
		dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {

			@Override
			public void onDismiss(DialogInterface dialog) {
				// TODO Auto-generated method stub

			}
		});
		// 监听Key事件被传递给dialog
		dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
			@Override
			public boolean onKey(DialogInterface dialog, int keyCode,
					KeyEvent event) {
				// TODO Auto-generated method stub
				System.out.println("setOnKeyListener");
				return false;
			}
		});
		/**
		 * 删除dialog时回调的方法
		 */
		dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {

			@Override
			public void onCancel(DialogInterface dialog) {
				// TODO Auto-generated method stub
				System.out.println("setOnCancelListener");
			}
		});
		//设置可点击的按钮,最多有三个(默认情况下)
		dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub

					}
				});
		dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub

					}
				});
		dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "中立",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub

					}
				});
		
		dialog.setMessage("这是一个圆形进度条");
		dialog.show();
		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					Thread.sleep(5000);
					// cancel和dismiss方法本质都是一样的,都是从屏幕中删除Dialog,唯一的区别是
					// 调用cancel方法会回调DialogInterface.OnCancelListener如果注册的话,dismiss                                        //方法不会回掉
					dialog.cancel();
					// dialog.dismiss();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}).start();
	}</span>

效果示意


自定义进度条

代码示例

创建布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minHeight="60dp"
    android:minWidth="180dp"
    android:gravity="center"
    android:padding="10dp"
    android:background="#FF5522"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"/>

</LinearLayout>

设置动画

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate       
        android:interpolator="@android:anim/linear_interpolator"	
        android:pivotX="50%"
        android:pivotY="50%"
        android:fromDegrees="0"
        android:toDegrees="+360"
        android:duration="1500"      
        android:startOffset="-1"
        android:repeatMode="restart"
        android:repeatCount="-1"/>
</set>

interpolator这里的属性表示匀速转动,pivotX表示绕X坐标的中点转动,startOffset表示动作间隔,这里-1表示无间隔

设置Dialog样式

    <!-- 自定义loading dialog -->
    <style name="loading_dialog" parent="android:style/Theme.Dialog">
		<item name="android:windowFrame">@null</item>
        <item name="android:windowNoTitle">true</item> 
        <item name="android:windowBackground">@drawable/ic_launcher</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
	</style>

应用

	@SuppressWarnings("deprecation")
	private void setDefinedDialog() {
		// TODO Auto-generated method stub
		LayoutInflater inflater = LayoutInflater.from(this);
		View v = inflater.inflate(R.layout.dialog_defined, null);
		LinearLayout layout = (LinearLayout) v.findViewById(R.id.linearlayout);
		imageView = (ImageView) v.findViewById(R.id.imageView);
		textView = (TextView) v.findViewById(R.id.textView1);
		// 加载动画
		Animation animation = AnimationUtils.loadAnimation(this,
				R.anim.loading_animation);
		// 使用imageView显示动画
		imageView.startAnimation(animation);
		textView.setText("提示");
		// 创建自定义样式dialog
		Dialog dialog = new Dialog(this, R.style.loading_dialog);
		dialog.setCancelable(true);
		dialog.setContentView(layout, new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.FILL_PARENT));
		dialog.show();
	}

效果示意


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值