Android控件之ProgressDialog

来源:互联网 发布:js弹窗广告代码 编辑:程序博客网 时间:2024/05/16 14:47

水平进度条

代码示例

<span style="font-size:18px;">private void setHorDialog() {// TODO Auto-generated method stubfinal 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 stubToast.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 stubToast.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 stubToast.makeText(MainActivity.this, "BUTTON_POSITIVE",Toast.LENGTH_SHORT).show();}});dialog.show();new Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubint i=0;while(i<100){try {Thread.sleep(200);//每次加1dialog.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 stubfinal 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() {@Overridepublic void onDismiss(DialogInterface dialog) {// TODO Auto-generated method stub}});// 监听Key事件被传递给dialogdialog.setOnKeyListener(new DialogInterface.OnKeyListener() {@Overridepublic boolean onKey(DialogInterface dialog, int keyCode,KeyEvent event) {// TODO Auto-generated method stubSystem.out.println("setOnKeyListener");return false;}});/** * 删除dialog时回调的方法 */dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {@Overridepublic void onCancel(DialogInterface dialog) {// TODO Auto-generated method stubSystem.out.println("setOnCancelListener");}});//设置可点击的按钮,最多有三个(默认情况下)dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub}});dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub}});dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "中立",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub}});dialog.setMessage("这是一个圆形进度条");dialog.show();new Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubtry {Thread.sleep(5000);// cancel和dismiss方法本质都是一样的,都是从屏幕中删除Dialog,唯一的区别是// 调用cancel方法会回调DialogInterface.OnCancelListener如果注册的话,dismiss                                        //方法不会回掉dialog.cancel();// dialog.dismiss();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.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 stubLayoutInflater 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("提示");// 创建自定义样式dialogDialog 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();}

效果示意


0 0
原创粉丝点击