Android开发复用代码

Posted android小鑫

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android开发复用代码相关的知识,希望对你有一定的参考价值。

1.设置状态栏透明

    if (Build.VERSION.SDK_INT >= 21)
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
            int ui = decorView.getSystemUiVisibility();
            ui |=View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; //设置状态栏中字体的颜色为白色
            decorView.setSystemUiVisibility(ui);
        
if (Build.VERSION.SDK_INT >= 21)
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            getWindow().setStatusBarColor(Color.BLACK);
            int ui = decorView.getSystemUiVisibility();
            ui |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN; //设置状态栏中字体的颜色为黑色
            decorView.setSystemUiVisibility(ui);
        

2.Dialog样式一(是/否)

private void showNormalDialog(final String xid)
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(alldata.this);
        normalDialog.setTitle("删除");
        normalDialog.setMessage("确认是否删除?");
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() 
                    @Override
                    public void onClick(DialogInterface dialog, int which) 
                        del(xid);
                    
                );
        normalDialog.setNegativeButton("关闭",
                new DialogInterface.OnClickListener() 
                    @Override
                    public void onClick(DialogInterface dialog, int which) 
                        Toast.makeText(alldata.this, "关闭", Toast.LENGTH_SHORT).show();
                    
                );
        // 显示
        normalDialog.show();
    

2.1 Dialog样式二(新手引导)

private void newYingdao() 
        LayoutInflater inflater = LayoutInflater.from(courseInfo.this);
        View view = inflater.inflate(R.layout.tips1, null);
        AlertDialog.Builder builder=new AlertDialog.Builder(courseInfo.this,R.style.TransparentDialog);
        builder.setView(view);
        final AlertDialog dialog=builder.create();
        final Button b=view.findViewById(R.id.button4);
        final LinearLayout tips=view.findViewById(R.id.tips);
        tips.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                dialog.dismiss();
            
        );
        b.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                dialog.dismiss();
            
        );
        dialog.show();

    
<style name="TransparentDialog" parent="Theme.AppCompat.Dialog">
        <item name="android:windowBackground">@color/transparent</item>
    </style>

2.2 Diaglog点击其他地方不销毁

 //创建dialog
                final AlertDialog dialog=builder.create();
                //dialog点击其他地方不关闭
                dialog.setCancelable(false);

3.从下方弹出页面

 private void dialogs2() 
        LayoutInflater inflater = LayoutInflater.from(getApplication());
        View view = inflater.inflate(R.layout.home_ulist, null);
        AlertDialog.Builder builder=new AlertDialog.Builder(home.this);
        builder.setView(view);
        final AlertDialog dialog=builder.create();
        Window window = dialog.getWindow();
        final TextView bianji=view.findViewById(R.id.bianji);
        users(bianji);
        window.setGravity(Gravity.BOTTOM);  //此处可以设置dialog显示的位置
        window.setWindowAnimations(R.style.SelectUpload2);  //添加动画
        dialog.show();
        WindowManager.LayoutParams params =
                dialog.getWindow().getAttributes();
        WindowManager wm = (WindowManager)home.this.getSystemService(Context.WINDOW_SERVICE);
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        dialog.getWindow().setBackgroundDrawableResource(R.color.white);
        dialog.getWindow().setAttributes(params);
    
<style name="SelectUpload2">

        <item name="android:windowEnterAnimation">@anim/dialog_enter</item>  //进入时的动画
        <item name="android:windowExitAnimation">@anim/dialog_exit</item>    //退出时的动

    </style>

4.封装的PrefStore

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class PrefStore 

    private static final String STORE_NAME = "Settings";
    private static Context mContext = null;
    private static PrefStore instance = null;

    public static PrefStore getInstance(Context context) 
        if (instance == null) 
            instance = new PrefStore(context);
        
        return instance;
    

    public PrefStore(Context context) 
        mContext = context.getApplicationContext();
    

    public boolean savePref(String key, String value) 

        if (mContext != null) 
            SharedPreferences pref = mContext.getSharedPreferences(STORE_NAME,
                    Context.MODE_PRIVATE);
            Editor editor = pref.edit();
            editor.putString(key, value);
            editor.commit();
            return true;
         else 
            return false;
        

    

    public String getPref(String key, String defValue) 

        if (mContext != null) 
            SharedPreferences pref = mContext.getSharedPreferences(STORE_NAME,
                    Context.MODE_PRIVATE);
            return pref.getString(key, defValue);
         else 
            return null;
        

    

    public boolean removePref(String key) 
        if (mContext != null) 
            SharedPreferences pref = mContext.getSharedPreferences(STORE_NAME,
                    Context.MODE_PRIVATE);
            Editor editor = pref.edit();
            editor.remove(key);
            editor.commit();
            return true;
         else 
            return false;
        
    

    public boolean clearPref() 
        if (mContext != null) 
            SharedPreferences pref = mContext.getSharedPreferences(STORE_NAME,
                    Context.MODE_MULTI_PROCESS);
            Editor editor = pref.edit();
            editor.clear();
            editor.commit();
            return true;
         else 
            return false;
        
    



4.1 封装的PrefStore使用

PrefStore pref = PrefStore.getInstance(Login.this);
pref.savePref("Phone", USERS.getPhone());
pref.getPref("Uid","1")

5.封装的PicassoUtils

PicassoUtils.loadImageViewCrop2(ChangeHeadImage.this,localhost_path+ result, imageView);

6.封装的OkHttpUtils

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * OkHttp的封装工具类
 */
public class OkHttpUtils 
    //TAG
    private static final String TAG = OkHttpUtils.class.getSimpleName();
    //声明客户端
    private OkHttpClient client;
    //防止多个线程同时访问所造成的安全隐患
    private volatile static OkHttpUtils okHttpUtils;
    //定义提交类型Json
    private static final MediaType JSON = MediaType.parse("application/json;charset=utf-8");
    //定义提交类型String
    private static final MediaType STRING = MediaType.parse("text/x-markdown;charset=utf-8");
    //子线程
    private Handler handler;
    //构造方法
    private OkHttpUtils() 
        //初始化
        client = new OkHttpClient();
        handler = new Handler(Looper.getMainLooper());
    
    //单例模式
    public static OkHttpUtils getInstance() 
//        OkHttpUtils okUtils = null;
//        if (okHttpUtils == null) 
//            //线程同步
//            synchronized (OkHttpUtils.class) 
//                if (okUtils == null) 
//                    okUtils = new OkHttpUtils();
//                    okHttpUtils = okUtils;
//                
//            
//        
        OkHttpUtils okUtils = new OkHttpUtils();
        return okUtils;
    
    /**
     * 请求的返回结果是json字符串
     * @param jsonValue
     * @param callBack
     */
    private void onsuccessJsonStringMethod(final String jsonValue, final FuncJsonString callBack) 
        handler.post(new Runnable() 
            @Override
            public void run() 
                if (callBack != null) 
                    try 
                        //解析json
                        callBack.onResponse(jsonValue);
                     catch (Exception e) 

                    
                
            
        );
    
    /**
     * 求的返回结果是json对象
     * @param jsonValue
     * @param callBack
     */
    private void onsuccessJsonObjectMethod(final String jsonValue, final FuncJsonObject callBack) 
        handler.post(new Runnable() 
            @Override
            public void run() 
                if (callBack != null) 
                    try 
                        callBack.onResponse(new JSONObject(jsonValue));
                     catch (JSONException e) 
                        e.printStackTrace();
                    
                
            
        );
    
    /**
     * 求的返回结果是json数组
     * @param data
     * @param callBack
     */
    private void onsuccessJsonByteMethod(final byte[] data, final FuncJsonObjectByte callBack) 
        handler.post(new Runnable() 
            @Override
            public void run() 
                if (callBack != null) 
                    callBack.onResponse(data);
                
            
        );
    
    /**
     * 同步请求,不是很常用,因为会阻塞线程
     * @param url
     * @return
     */
    public String syncGetByURL(String url) 
        //构建一个Request请求
        Request request = new Request.Builder().url(url).build();
        Response response = null;
        try 
            //同步请求数据
            response = client.newCall(request).execute以上是关于Android开发复用代码的主要内容,如果未能解决你的问题,请参考以下文章

转载不可不知的 Android strings.xml 那些事

React-Native开发之原生模块封装(Android)升级版

写的Android代码不能复用?从MVC开始重新学习

写的Android代码不能复用?从MVC开始重新学习

Android开发之MVP模式的使用

【Android开发】怎么在ListView中做一个图片批量上传的队列