(初学者)回调技能学习之工具类执行回调

来源:互联网 发布:linux下导入dmp文件 编辑:程序博客网 时间:2024/06/05 16:59

   hello,大家好,我是超级康,是一名博客的新生,这篇博客是我的首篇技术博客,希望大家能学习到些许知识。

    在公司上班接近半年的时间,关于回调这个技能,在项目中真的是屡见不鲜。接下来,我会就项目中工具类中所用到的回调进行一些总结。

 为了能够贴切的感受回调,我们直接新建一个AndroidStudio项目 -- UploadCallbackDemo

 张贴主界面activity_main代码:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="center"    >    <TextView
android:id="@+id/tv"
android:layout_width="60dp" android:layout_height="40dp" android:background="#ccc" android:gravity="center" android:text="上传" /></RelativeLayout>

 正如我们所写代码所示,整个界面中只是采用了一个TextView控件,而此控件的值为“上传”。即为下图所示:


接下来为主界面的代码实现:


在界面中主要实现了该控件的点击事件--我们现在的需求是在点击该控件后,调起工具类UploadHelper进行上传相关业务逻辑的时候,在上传完毕后,调起回调,弹出提示“上传完毕”。

引入工具类LoadingDialog 用于在上传时显示该加载窗口

public class LoadingDialog {   public static Dialog createLoadingDialog(Context context) {      return createLoadingDialog(context, context.getResources().getString(R.string.loading));   }   /**    * 得到自定义的progressDialog    *     * @param context    * @param msg    * @return    */   public static Dialog createLoadingDialog(Context context, String msg) {      LayoutInflater inflater = LayoutInflater.from(context);      View v = inflater.inflate(R.layout.loadingdialogactivity, null);// 得到加载view      // LinearLayout layout = (LinearLayout)      // v.findViewById(R.id.dialog_view);// 加载布局      // main.xml中的ImageView      // ImageView spaceshipImage = (ImageView) v.findViewById(R.id.img);      TextView tipTextView = (TextView) v.findViewById(R.id.tv_loading);      // 提示文字      // 加载动画      // Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(      // context, R.anim.load_animation);      // 使用ImageView显示动画      // spaceshipImage.startAnimation(hyperspaceJumpAnimation);      tipTextView.setText(msg);// 设置加载信息      Dialog loadingDialog = new Dialog(context, R.style.MyDialogStyle);// 创建自定义样式dialog      loadingDialog.setCancelable(false);// 不可以用返回键取消      loadingDialog.setContentView(v, new LinearLayout.LayoutParams(            LinearLayout.LayoutParams.FILL_PARENT,            LinearLayout.LayoutParams.FILL_PARENT));// 设置布局      loadingDialog.setCancelable(true);//是否可以关闭该对话框      return loadingDialog;   }}
//------------源代码将在github进行展示----------------//

接下来我们进行编写工具类UploadHelper

/** * Created by Qk on 2017/4/24. * 邮箱: */public class UploadHelper {    private Context context;    public UploadHelper(Context context) {        this.context = context;    }    /**     * 执行上传业务逻辑     *     * @param callBack     */    public void upload(final UploadCallback callBack) {        new Handler().postDelayed(new Runnable() {            @Override            public void run() {                if (callBack != null) {                    callBack.endUpload();//调用结束上传                }            }        }, 5000);    }}


随后在主界面中的点击时间中进行调用该方法,采用延迟加载的方法来模拟在上传文件,我们能够看到使用了callBack.endUpload()方法来实现方法的回调


在方法调用完毕后,代码的走向会走到主界面中红的endUpload方法实现体中。接下来就会弹出提示“上传结束”,并结束旋转的弹窗。


博客主要讲解了回调的一个简单用法,希望大家能够对回调有一个初步的了解,本人也是安卓新生,有问题的话,大家可以在下方评论区进行沟通交流,谢谢~




0 0
原创粉丝点击