// popuWindow

private void chooseType() {
        View v = mInflater.inflate(R.layout.view_cash_type_pop, null);
        final PopupWindow popupWindow = new PopupWindow(v, DpUtil.dp2px(60), ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(ContextCompat.getDrawable(mContext, R.drawable.bg_pop_cash));
        TextView btn1 = v.findViewById(R.id.btn_1);
        TextView btn2 = v.findViewById(R.id.btn_2);
        if (mKey == Constants.CASH_ACCOUNT_ALI) {
            btn1.setTag(mSparseIntArray.keyAt(1));
            btn1.setText(mSparseIntArray.valueAt(1));
            btn2.setTag(mSparseIntArray.keyAt(2));
            btn2.setText(mSparseIntArray.valueAt(2));
        } else if (mKey == Constants.CASH_ACCOUNT_WX) {
            btn1.setTag(mSparseIntArray.keyAt(0));
            btn1.setText(mSparseIntArray.valueAt(0));
            btn2.setTag(mSparseIntArray.keyAt(2));
            btn2.setText(mSparseIntArray.valueAt(2));
        } else {
            btn1.setTag(mSparseIntArray.keyAt(0));
            btn1.setText(mSparseIntArray.valueAt(0));
            btn2.setTag(mSparseIntArray.keyAt(1));
            btn2.setText(mSparseIntArray.valueAt(1));
        }
        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                Object tag = v.getTag();
                if (tag != null) {
                    int key = (int) tag;
                    mBtnChooseType.setText(mSparseIntArray.get(key));
                    mKey = key;
                    switch (key) {
                        case Constants.CASH_ACCOUNT_ALI:
                            if (mGroup1.getVisibility() != View.VISIBLE) {
                                mGroup1.setVisibility(View.VISIBLE);
                            }
                            if (mGroup2.getVisibility() == View.VISIBLE) {
                                mGroup2.setVisibility(View.GONE);
                            }
                            if (mGroup3.getVisibility() == View.VISIBLE) {
                                mGroup3.setVisibility(View.GONE);
                            }
                            break;
                        case Constants.CASH_ACCOUNT_WX:
                            if (mGroup1.getVisibility() == View.VISIBLE) {
                                mGroup1.setVisibility(View.GONE);
                            }
                            if (mGroup2.getVisibility() != View.VISIBLE) {
                                mGroup2.setVisibility(View.VISIBLE);
                            }
                            if (mGroup3.getVisibility() == View.VISIBLE) {
                                mGroup3.setVisibility(View.GONE);
                            }
                            break;
                        case Constants.CASH_ACCOUNT_BANK:
                            if (mGroup1.getVisibility() == View.VISIBLE) {
                                mGroup1.setVisibility(View.GONE);
                            }
                            if (mGroup2.getVisibility() == View.VISIBLE) {
                                mGroup2.setVisibility(View.GONE);
                            }
                            if (mGroup3.getVisibility() != View.VISIBLE) {
                                mGroup3.setVisibility(View.VISIBLE);
                            }
                            break;
                    }
                }
            }
        };
        btn1.setOnClickListener(onClickListener);
        btn2.setOnClickListener(onClickListener);
        popupWindow.showAsDropDown(mBtnChooseType);
    }

// popupWindow 点击返回键 back 没有反应(popuwindow)

popupwindow设置一个参数:参考文章

popupWindow.setBackgroundDrawable(new BitmapDrawable());

// 动态设置 TextView drawable

public static void setTop(Context context, TextView textView, int resourceId) {
    Drawable drawable = ContextCompat.getDrawable(context, resourceId);
    drawable.setBounds(new Rect(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()));
    textView.setCompoundDrawables(null, drawable, null, null);
}

// Android Home 按键,拦截Home按键,模拟Home按键

参考:Android——实现Home键功能_程序员大佬超的博客-CSDN博客

 Intent intent= new Intent(Intent.ACTION_MAIN);
 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 intent.addCategory(Intent.CATEGORY_HOME);
 startActivity(intent);
Activity.super.moveTaskToBack(true);

// 竖向滑动的viewpager

VerticalViewPager.txt

// Android 获取设备唯一值,设备号

参考:android 获取设备唯一值_xieyaofeng的博客-CSDN博客_android 唯一值

/***
 * 获取CPUid
 * @return
 */
public static String getCpuInfo() {
    String cpuInfo = "";
    try {
        File file = new File("/proc/cpuinfo");
        FileReader reader = new FileReader(file);
        char[] bb = new char[1024];
        int n;
        while ((n = reader.read(bb)) != -1) {
            cpuInfo += new String(bb, 0, n);
        }
        reader.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (!TextUtils.isEmpty(cpuInfo) || cpuInfo.length() > 2) {
        cpuInfo = cpuInfo.substring(cpuInfo.lastIndexOf(":") + 1, cpuInfo.length()).trim();
    }
    return cpuInfo;
}
/***
 * 获取设备的序列号
 * @return
 */
private static String getSerialNumber() {
    String serial = null;
    try {
        Class<?> c = Class.forName("android.os.SystemProperties");
        Method get = c.getMethod("get", String.class);
        serial = (String) get.invoke(c, "ro.serialno");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return serial;
}

// 本地存储 icon 取出来 设置到view上

//   本地存储 icon
//   本地存储 取出来 int resID = getResources().getIdentifier(icon, "mipmap",getActivity().getPackageName());
//   本地存储 设置到view上 pic.setImageDrawable(getResources().getDrawable(resID));

// 字符串转list

JsonUtil.zip

/**
 * 解析json数组为ArrayList
 *
 * @param json  要解析的json
 * @param clazz 解析类
 * @return ArrayList
 */
public static <T> ArrayList<T> parseToArrayList(String json, Class<T> clazz) {
    Type type = new TypeToken<ArrayList<JsonObject>>() {
    }.getType();
    ArrayList<JsonObject> jsonObjects = new Gson().fromJson(json, type);
    ArrayList<T> arrayList = new ArrayList<>();
    for (JsonObject jsonObject : jsonObjects) {
        arrayList.add(new Gson().fromJson(jsonObject, clazz));
    }
    return arrayList;
}

// 列表list去重

public static List<HistoryBean> removeDuplicate(List<HistoryBean> li2) {
    for (int i = 0; i < li2.size(); i++) { //外循环是循环的次数
        for (int j = li2.size() - 1; j > i; j--) { //内循环是 外循环一次比较的次数
            if (li2.get(i).getWord().equals(li2.get(j).getWord())) {
                li2.remove(j);
            }
        }
    }
    return li2;
}

// 查看 sqlite 数据库表中内容方法

参考:Android Studio查看SQLite数据库方法大全

使用 Stetho

// Stetho是一个可以在线调试数据库的方法,这种方法是最方便和简单
implementation 'com.facebook.stetho:stetho:1.5.1'

Stetho.initializeWithDefaults(this);

如果打开空白页,在hosts中加入代码。关闭浏览器,然后重试
61.91.161.217 chrome-devtools-frontend.appspot.com
61.91.161.217 chrometophone.appspot.com

// 修改手机设置导航栏和状态栏的颜色的。

 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private void setSystemBar(@ColorRes int statusBar) {
        if (statusBar == 0) {
            StatusBarUtil.setDarkMode(this);
            StatusBarUtil.setColor(this, getResources().getColor(R.color.colorPrimary), 0);
            setLightStatusBar(getWindow(),false);
        } else if (statusBar == R.color.transparent) {
            StatusBarUtil.setDarkMode(this);
            StatusBarUtil.setColor(this, getResources().getColor(statusBar), 0);
            setLightStatusBar(getWindow(),false);
        } else {
            StatusBarUtil.setLightMode(this);
            StatusBarUtil.setColor(this, getResources().getColor(statusBar), 0);
            setLightStatusBar(getWindow(),true);
        }
        if (ActivityCollector.getCurrentActivity() instanceof ChatActivity) {
            getWindow().setNavigationBarColor(getResources().getColor(R.color.white));
        } else if (ActivityCollector.getCurrentActivity() instanceof MainActivity) {
            getWindow().setNavigationBarColor(getResources().getColor(R.color.color_f8f8f8));
        } else {
            getWindow().setNavigationBarColor(getResources().getColor(R.color.transparent));
        }
        setLightNavigationBar(getWindow(),true);
    }

    /**
     * 状态栏显示
     * */
    public static void setLightStatusBar(Window window, boolean enable) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            final View decorView = window.getDecorView();
            int flags = decorView.getSystemUiVisibility();
            if (enable) {
                if ((flags & View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR) == 0) {
                    flags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
                    decorView.setSystemUiVisibility(flags);
                }
            } else {
                if ((flags & View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR) != 0) {
                    flags &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
                    decorView.setSystemUiVisibility(flags);
                }
            }
        }
    }

    /**
     * 导航栏显示
     * */
    public static void setLightNavigationBar(Window window, boolean enable) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            final View decorView = window.getDecorView();
            int flags = decorView.getSystemUiVisibility();
            if (enable) {
                flags |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
            } else {
                flags &= ~View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
            }
            decorView.setSystemUiVisibility(flags);
        }
    }

// java 批量重命名文件名

import java.io.File;
import java.io.*;

public class ReFilesName {
	public static void main(String[] args) {
		  // String filePath = "D:\\WorkSpace\\CampusMeal\\app\\src\\main\\res\\mipmap-xhdpi2"; // set path
		  String filePath = ".\\"; // current path
		  File[] fileList = ReFilesName.getCurFilesList(filePath);
		// File[] fileList = ReFilesName.getProperty("user.dir");
		for (File file : fileList) {
			 File nf = new File("icon_"+file.getName()); // rename file name
			 file.renameTo(nf); // rename file name
			System.out.println(nf.getName());
		}
	}
			
	public static File[] getCurFilesList(String filePath) {
		File path = new File(filePath);
		File[] listFiles = path.listFiles(new java.io.FileFilter() {
			
			@Override
			public boolean accept(File pathname) {
				if (pathname.isFile())
					return true;
				else
					return false;
			}
		});

		return listFiles;
	}
}

// github 访问慢,无法访问

#github
140.82.113.4 github.com
199.232.5.194 github.global.ssl.fastly.net

// 生成apk自定义名称,打包自定义apk名称

参考:(1条消息) android studio 打包自动生成版本号与日期,apk输入路径_Swer-CSDN博客

参考:(1条消息) Error:(46, 0) Could not get unknown property ‘outputFile’ for object of type com.android.build.gr…_小码农大理想-CSDN博客

参考:Android Studio打包全攻略

// build.gradle app
// 适用于 classpath 'com.android.tools.build:gradle:4.0.0'
android {
    applicationVariants.all {
    variant ->
        variant.outputs.all {
            //只处理生产版本
            if (buildType.name == 'release') {
                  def createTime = new Date().format("YYYY-MM-dd-hh-mm-ss", TimeZone.getTimeZone("GMT+08:00"))
                def createTime = new Date().format("YYYY-MM-dd-hhmmss", TimeZone.getTimeZone("GMT+08:00"))
                // app包名称
                it.outputFileName = "xxxxxxx" + "-" + createTime + "-" + buildType.name + defaultConfig.versionName + ".apk"
                // 指定release输出到指定文件夹
                  variant.getPackageApplication().outputDirectory = new File(config.appReleaseDir + "/${createTime}")
            }
        }
    }
}

 

作者 wanglei

发表评论