2017.6.26 工作记录

Android 中传参数的时候经常需要传参数,有时候参数需要通过bundle进行传递,但每次都要写如下显得非常麻烦

Bundle bundle = new Bundle();
bundle.putSerializable("key2",...);
bundle.putString("strKey",...);
bundle.putInt("intKey",...);

所以我就写了个工具类中使用的方法,只需传参数就可以了,方法暂时只支持String,int,boolean,实现接口的Serializable和Parcelable,欢迎使用这个方法

public static ArrayList<String> bundleKeys;
    /**
     * 获取Bundle
     * @param objs
     * @return
     */
    public static Bundle getBundle(Object... objs){
        Bundle bundle = new Bundle();
        String key = "";
        bundleKeys = new ArrayList<>();
        for(int i = 0; i< objs.length ;i++){
            key = "obj"+(i+1);
            bundleKeys.add(key);
            if(objs[i] instanceof Integer){
                bundle.putInt(key, (Integer) objs[i]);
                continue;
            }
            if(objs[i] instanceof String){
                bundle.putString(key, (String) objs[i]);
                continue;
            }
            if(objs[i] instanceof Boolean){
                bundle.putBoolean(key, (Boolean) objs[i]);
                continue;
            }
            if(objs[i] instanceof Serializable){
                bundle.putSerializable(key, (Serializable) objs[i]);
                continue;
            }
            if(objs[i] instanceof Parcelable){
                bundle.putParcelable(key, (Parcelable) objs[i]);
                continue;
            }
        }
        return bundle;
    }

使用方法:Util.getBundle("string",true,2);  

解析端:

String str = getIntent().getExtras().getString(Uril.bundleKeys.get(0));
Boolean bool = getIntent().getExtras().getBoolean(Uril.bundleKeys.get(1))
int num = getIntent().getExtras().getInt(Uril.bundleKeys.get(2))
原文地址:https://www.cnblogs.com/woaixingxing/p/7081154.html