Weex Android SDK源码分析之Module(modal)

前言

当您来阅读这篇文章时,代表您已经是weex粉丝了,我说的对吗?(-_-) 可是您只会使用可不行,本篇博文介绍Weex Moudle中的model 使用与源码分析。

代码分析

Weex封装了一系列的model api,例如:toast, alert, confirm and prompt.

Toast

weex code

使用:

this.$call('modal', 'toast', {
        'message': content,
        'duration': 2.0
    });


Arguments: 

    message、duration;

example: 

    var modal = require('@weex-module/modal');
    modal.toast({'message': 'I am toast!', 'duration': 1});

android method

    private Toast toast;

    @WXModuleAnno
    public void toast(String param) {
        String message = "";
        int duration = Toast.LENGTH_SHORT;
        if (!TextUtils.isEmpty(param)) {
            try {
                param = URLDecoder.decode(param, "utf-8");
                JSONObject jsObj = new JSONObject(param);
                message = jsObj.optString(WXConst.MESSAGE);
                duration = jsObj.optInt(WXConst.DURATION);
            } catch (Exception e) {
                WXLogUtils.e("[WXModalUIModule] alert param parse error "+WXLogUtils.getStackTrace(e));
            }
        }
        if (TextUtils.isEmpty(message)) {
            WXLogUtils.e("[WXModalUIModule] toast param parse is null ");
            return;
        }

        if (duration > 3) {
            duration = Toast.LENGTH_LONG;
        } else {
            duration = Toast.LENGTH_SHORT;
        }
        if (toast == null) {
            toast = Toast.makeText(mWXSDKInstance.getContext(), message, duration);
        } else {
            toast.setDuration(duration);
            toast.setText(message);
        }
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }
1、utf-8 解码;
2、取出message值;
3、取出duration值;
4、位置居中;

alert

weex code

Arguments :

    message(string): 显示内容.
    okTitle(string): 确认按钮.
    callback(function): 弹出回调.

Example :

    var arg1 = 'I am alert!';
    var arg2 = 'I am ok';
    var modal = require('@weex-module/modal');
    modal.alert({ message: arg1, okTitle: arg2 }, function(e) { // TODO after the alert is complete. })

android method

    @WXModuleAnno
    public void alert(String param, final String callbackId) {
        if (mWXSDKInstance.getContext() instanceof Activity) {
            String message = "";
            String okTitle = WXConst.OK;
            if (!TextUtils.isEmpty(param)) {
                try {
                    param = URLDecoder.decode(param, "utf-8");
                    JSONObject jsObj = new JSONObject(param);
                    message = jsObj.optString(WXConst.MESSAGE);
                    okTitle = jsObj.optString(WXConst.OK_TITLE);
                } catch (Exception e) {
                    WXLogUtils.e("[WXModalUIModule] alert param parse error " + WXLogUtils.getStackTrace(e));
                }
            }
            if (TextUtils.isEmpty(message)) {
                WXLogUtils.e("[WXModalUIModule] alert param parse is null ");
                return;
            }
            AlertDialog.Builder builder = new AlertDialog.Builder(mWXSDKInstance.getContext());
            builder.setMessage(message);

            final String okTitle_f = TextUtils.isEmpty(okTitle) ? WXConst.OK : okTitle;
            builder.setPositiveButton(okTitle_f, new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    WXBridgeManager.getInstance().callback(mWXSDKInstance.getInstanceId(), callbackId, okTitle_f);
                }
            });
            AlertDialog alertDialog = builder.create();
            alertDialog.setCanceledOnTouchOutside(false);
            alertDialog.show();
        } else {
            WXLogUtils.e("[WXModalUIModule] when call alert mWXSDKInstance.getContext() must instanceof Activity");
        }
    }
1、utf-8 解码;
2、取出message值;
3、取出title值;
4、点击回调callback;

confirm

weex code

Arguments

    message(string): the message that the confirm shows.
    okTitle(string): the title of confirm button.
    cancelTitle(string): the title of cancel button.
    callback(function): callback when complete.
Example

    var arg1 = 'I am alert!'
    var arg2 = 'I am ok'
    var arg3 = 'I am cancel'
    var modal = require('@weex-module/modal');
    modal.confirm({ message: arg1, okTitle: arg2, cancelTitle: arg3 }, function(e) { nativeLog(e.status) // TODO after the confirm is complete. });

android method

    @WXModuleAnno
    public void confirm(String param, final String callbackId) {
        if (mWXSDKInstance.getContext() instanceof Activity) {
            String message = "";
            String okTitle = WXConst.OK;
            String cancelTitle = WXConst.CANCEL;
            if (!TextUtils.isEmpty(param)) {
                try {
                    param = URLDecoder.decode(param, "utf-8");
                    JSONObject jsObj = new JSONObject(param);
                    message = jsObj.optString(WXConst.MESSAGE);
                    okTitle = jsObj.optString(WXConst.OK_TITLE);
                    cancelTitle = jsObj.optString(WXConst.CANCEL_TITLE);
                } catch (Exception e) {
                    WXLogUtils.e("[WXModalUIModule] confirm param parse error " + WXLogUtils.getStackTrace(e));
                }
            }
            if (TextUtils.isEmpty(message)) {
                WXLogUtils.e("[WXModalUIModule] confirm param parse is null ");
                return;
            }
            AlertDialog.Builder builder = new AlertDialog.Builder(mWXSDKInstance.getContext());
            builder.setMessage(message);

            final String okTitle_f = TextUtils.isEmpty(okTitle) ? WXConst.OK : okTitle;
            final String cancelTitle_f = TextUtils.isEmpty(cancelTitle) ? WXConst.CANCEL : cancelTitle;

            builder.setPositiveButton(okTitle_f, new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    WXBridgeManager.getInstance().callback(mWXSDKInstance.getInstanceId(), callbackId, okTitle_f);
                }
            });
            builder.setNegativeButton(cancelTitle_f, new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    WXBridgeManager.getInstance().callback(mWXSDKInstance.getInstanceId(), callbackId, cancelTitle_f);
                }
            });
            AlertDialog alertDialog = builder.create();
            alertDialog.setCanceledOnTouchOutside(false);
            alertDialog.show();
        } else {
            WXLogUtils.e("[WXModalUIModule] when call confirm mWXSDKInstance.getContext() must instanceof Activity");
        }
    }
1、utf-8 解码;
2、取出message值;
3、取出cancel title值;
4、取出confirm title值;
5、点击回调callback;

prompt

weex code

Arguments

    message(string): the message that the prompt shows.
    okTitle(string): the title of confirm button.
    cancelTitle(string): the title of cancel button.
    callback(function): callback when complete.

    //返回参数 状态(字符串):单击用户单击的按钮的标题。
    // 数据(字符串):由用户输入的文本的值。

Example

var arg1 = 'I am prompt!'
var arg2 = 'I am ok'
var arg3 = 'I am cancel'
var modal = require('@weex-module/modal');
modal.prompt({ message: arg1, okTitle: arg2, cancelTitle: arg3 }, function(e) { nativeLog(e.status + ', ' + e.data); // TODO after the prompt is complete. });

android method

    @WXModuleAnno
    public void prompt(String param, final String callbackId) {
        if (mWXSDKInstance.getContext() instanceof Activity) {
            String message = "";
            String defaultValue = "";
            String okTitle = WXConst.OK;
            String cancelTitle = WXConst.CANCEL;

            if (!TextUtils.isEmpty(param)) {
                try {
                    param = URLDecoder.decode(param, "utf-8");
                    JSONObject jsObj = new JSONObject(param);
                    message = jsObj.optString("message");
                    okTitle = jsObj.optString("okTitle");
                    cancelTitle = jsObj.optString("cancelTitle");
                    defaultValue = jsObj.optString("default");
                } catch (Exception e) {
                    WXLogUtils.e("[WXModalUIModule] confirm param parse error " + WXLogUtils.getStackTrace(e));
                }
            }

            if (TextUtils.isEmpty(message)) {
                WXLogUtils.e("[WXModalUIModule] confirm param parse is null ");
                return;
            }
            AlertDialog.Builder builder = new AlertDialog.Builder(mWXSDKInstance.getContext());
            builder.setMessage(message);

            final EditText editText = new EditText(mWXSDKInstance.getContext());
            editText.setText(defaultValue);
            builder.setView(editText);
            final String okTitle_f = TextUtils.isEmpty(okTitle) ? WXConst.OK : okTitle;
            final String cancelTitle_f = TextUtils.isEmpty(cancelTitle) ? WXConst.CANCEL : cancelTitle;
            builder.setPositiveButton(okTitle_f, new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Map<String, Object> result = new HashMap<String, Object>();
                    result.put(WXConst.RESULT, okTitle_f);
                    result.put(WXConst.DATA, editText.getText().toString());
                    WXBridgeManager.getInstance().callback(mWXSDKInstance.getInstanceId(), callbackId, result);

                }
            });
            builder.setNegativeButton(cancelTitle_f, new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    WXBridgeManager.getInstance().callback(mWXSDKInstance.getInstanceId(), callbackId, cancelTitle_f);
                }
            });
            AlertDialog alertDialog = builder.create();
            alertDialog.setCanceledOnTouchOutside(false);
            alertDialog.show();
        } else {
            WXLogUtils.e("when call prompt mWXSDKInstance.getContext() must instanceof Activity");
        }
    }
1、utf-8 解码;
2、取出message值;
3、取出cancel title值;
4、取出confirm title值;
5、点击获取输入内容回调callback;
    原文作者:Android源码分析
    原文地址: https://blog.csdn.net/walid1992/article/details/51706199
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞