SVProgressHUD的基本使用

来源:互联网 发布:淘宝经营类目怎么修改 编辑:程序博客网 时间:2024/05/17 04:17

SVProgressHUD 用法

SVProgressHUD 是一个第三方的控件,是一个弹出提示层,用来提示 网络加载 或 提示对错,看下面图,你就明白了:


 
那么,SVProgressHUD 都有什么特点呢:
 
1. 提示当前的状态,如:网络传输、提交中、操作成功或失败等等.
 
2. 可是设置提示的 pop layer 是否为 model,就是,提示的时间是否允许用户做其他操作
 
3. 可以设置 背景色 和 自定义提示的内容
 
4. 使用起来非常简洁,代码量非常少
 
那么如何使用 SVProgressHUD 呢:
在app目录下的build.gradle文件中
dependencies属性下面配置 
compile 'com.bigkoo:svprogresshud:1.0.6'就可以正常使用SVProgressHUD ,非常简单方便
不多说,下面直接上代码
首先是actiity_main.xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:orientation="vertical"    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:paddingBottom="@dimen/activity_vertical_margin">    <Button android:text="show" android:layout_width="match_parent"        android:layout_marginTop="5dp"        android:layout_height="50dp"        android:onClick="show"/>    <Button android:text="showWithMaskType" android:layout_width="match_parent"        android:layout_marginTop="5dp"        android:layout_height="50dp"        android:onClick="showWithMaskType"/>    <Button android:text="showWithStatus" android:layout_width="match_parent"        android:layout_marginTop="5dp"        android:layout_height="50dp"        android:onClick="showWithStatus"/>    <Button android:text="showInfoWithStatus" android:layout_width="match_parent"        android:layout_marginTop="5dp"        android:layout_height="50dp"        android:onClick="showInfoWithStatus"/>    <Button android:text="showSuccessWithStatus" android:layout_width="match_parent"        android:layout_marginTop="5dp"        android:layout_height="50dp"        android:onClick="showSuccessWithStatus"/>    <Button android:text="showErrorWithStatus" android:layout_width="match_parent"        android:layout_marginTop="5dp"        android:layout_height="50dp"        android:onClick="showErrorWithStatus"/>    <Button android:text="showWithProgress" android:layout_width="match_parent"        android:layout_marginTop="5dp"        android:layout_height="50dp"        android:onClick="showWithProgress"/></LinearLayout>
然后是MianActivity.java文件
package com.example.administrator.svprogresshud;import android.content.Context;import android.os.Handler;import android.os.Message;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.KeyEvent;import android.view.View;import android.widget.Toast;import com.bigkoo.svprogresshud.SVProgressHUD;import com.bigkoo.svprogresshud.listener.OnDismissListener;public class MainActivity extends AppCompatActivity {    private SVProgressHUD mSVProgressHUD;    int progress = 0;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mSVProgressHUD = new SVProgressHUD(this);        mSVProgressHUD.setOnDismissListener(new OnDismissListener() {            @Override            public void onDismiss(SVProgressHUD hud) {                Toast.makeText(getApplicationContext(),"dismiss",Toast.LENGTH_LONG).show();            }        });    }    public void show(View view){        mSVProgressHUD.show();    }    public void showWithMaskType(View view){        mSVProgressHUD.showWithMaskType(SVProgressHUD.SVProgressHUDMaskType.None);//        mSVProgressHUD.showWithMaskType(SVProgressHUD.SVProgressHUDMaskType.Black);//        mSVProgressHUD.showWithMaskType(SVProgressHUD.SVProgressHUDMaskType.BlackCancel);//        mSVProgressHUD.showWithMaskType(SVProgressHUD.SVProgressHUDMaskType.Clear);//        mSVProgressHUD.showWithMaskType(SVProgressHUD.SVProgressHUDMaskType.ClearCancel);//        mSVProgressHUD.showWithMaskType(SVProgressHUD.SVProgressHUDMaskType.Gradient);//        mSVProgressHUD.showWithMaskType(SVProgressHUD.SVProgressHUDMaskType.GradientCancel);    }    public void showWithStatus(View view){        mSVProgressHUD.showWithStatus("加载中...");    }    public void showInfoWithStatus(View view){        mSVProgressHUD.showInfoWithStatus("这是提示", SVProgressHUD.SVProgressHUDMaskType.None);    }    public void showSuccessWithStatus(View view){        mSVProgressHUD.showSuccessWithStatus("恭喜,提交成功!");    }    public void showErrorWithStatus(View view){        mSVProgressHUD.showErrorWithStatus("不约,叔叔我们不约~", SVProgressHUD.SVProgressHUDMaskType.GradientCancel);    }    private Handler mHandler = new Handler(){        @Override        public void handleMessage(Message msg) {            super.handleMessage(msg);            progress = progress + 5;            if (mSVProgressHUD.getProgressBar().getMax() != mSVProgressHUD.getProgressBar().getProgress()) {                mSVProgressHUD.getProgressBar().setProgress(progress);                mSVProgressHUD.setText("进度 "+progress+"%");                mHandler.sendEmptyMessageDelayed(0,500);            }            else{                mSVProgressHUD.dismiss();            }        }    };    public void showWithProgress(View view){        progress = 0;        mSVProgressHUD.getProgressBar().setProgress(progress);//先重设了进度再显示,避免下次再show会先显示上一次的进度位置所以要先将进度归0        mSVProgressHUD.showWithProgress("进度 " + progress + "%", SVProgressHUD.SVProgressHUDMaskType.Black);        mHandler.sendEmptyMessageDelayed(0,500);    }    @Override    public boolean onKeyDown(int keyCode, KeyEvent event)    {        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)        {            if(mSVProgressHUD.isShowing()){                mSVProgressHUD.dismiss();                mHandler.removeCallbacksAndMessages(null);                return false;            }        }        return super.onKeyDown(keyCode, event);    }}
大家运行看看效果吧

0 0