欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

杂乱无章的小笔记

程序员文章站 2022-05-08 09:58:25
...

1、通过strings资源改变部分字体颜色

<Data><![CDATA[<font color="#000000">这是需要改变颜色的字体</font>%这是正常的字体颜色<font color="#ffffff">这是需要改变颜色的字体</font>]]></Data>

此资源需要通过 Html.fromHtml() 来设置,否则无效

2、剪切板

复制文本到剪切板

public static void copyToClipboard(Context context, String copyText) {
        // 从API11开始android推荐使用android.content.ClipboardManager
        // 为了兼容低版本我们这里使用旧版的android.text.ClipboardManager,虽然提示deprecated,但不影响使用。
        ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        // 将文本内容放到系统剪贴板里。
        cm.setText(copyText);
}

监听剪切板的变化

final ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        cm.addPrimaryClipChangedListener(new ClipboardManager.OnPrimaryClipChangedListener() {
            @Override
            public void onPrimaryClipChanged() {
                ClipData primaryClip = cm.getPrimaryClip();
                ClipData.Item item = primaryClip.getItemAt(0);
                String text= item.getText().toString();  // 获得复制的文本
            }
        });

3、Dialog全屏时,设置状态栏透明

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        }

这时想要导航栏实现沉浸式效果还需设置dialog的样式

<!--背景颜色及和透明程度-->
<item name="android:windowBackground">@android:color/transparent</item>
<!--是否模糊-->
<item name="android:backgroundDimEnabled">false</item>

保留小数

保留2位小数,当入参不足2位小数时,自动补0,超出2位时不会四舍五入

Log.e("------------>",String.format("%.2f", 1.2));  //输出结果为 1.20
Log.e("------------>",String.format("%.2f", 1.234));  //输出结果为 1.23

保留2位小数,当入参不足2位小数时,不会补0,超出2位时进行四舍五入

NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);
Log.e("------------>",nf.format(1.246));  // 输出结果为1.25
Log.e("------------>",nf.format(1.2));  // 输出结果为1.2

##Edittext限制输入最大和最小值

public static void limitInput(final EditText editText, final double max, final double min, final Activity activity, final String msg) {
        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (StringUtils.isBlank(s.toString())) return;
                s = s.toString().replaceAll(",", "");
                if (max != -1 && min != -1) {
                    // 有多个小数点
                    if (s.toString().indexOf(".") != s.toString().lastIndexOf(".")) {
                        editText.setText("");
                        showToast(activity, msg);
                        return;
                    }
                    double num = Double.parseDouble(s.toString().equals("") || s.toString().equals(".") ? "0" : s.toString());
                    if (num > max) {
                        showToast(activity, msg);
                        s = s.subSequence(0, s.length() - 1);
                        editText.setText("");
                    } else if (num < min) {
                        s = String.valueOf(min);
                        editText.setText("");
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
                // 保留两位小数
                String temp = editable.toString();
                if (temp == null) return;
                int posDot = temp.indexOf(".");
                if (posDot <= 0) return;
                if (temp.length() - posDot - 1 > 2) {
                    editable.delete(posDot + 3, posDot + 4);
                }
            }
        };
        editText.addTextChangedListener(textWatcher);
    }

ScrollView嵌套RecyclerView动态计算item高度

    /** row: recyclerView有几列 */
    private void recyclerRefreshHight(RecyclerView.Adapter adapter, RecyclerView recyclerView, int row) {
        try {
            //获取标题项
//            ReserverPictureAdapter adapter = (ReserverPictureAdapter) recyclerView.getAdapter();

            int dataCount = adapter.getItemCount();
            if (dataCount > 1) {
                //获取第一个子项
                View childAt = recyclerView.getLayoutManager().getChildAt(0);
                ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) recyclerView.getLayoutParams();
                if (itemHeight == 0) {
                    //子项高度
                    itemHeight = childAt.getMeasuredHeight() + marginLayoutParams.topMargin + marginLayoutParams.bottomMargin;
                }
                //计算出子项行数
                int size = dataCount / row;
                if ((dataCount % row) > 0) {
                    size++;
                }
                //算出列表高度
                int i = itemHeight * size;
                ViewGroup.LayoutParams layoutParams = recyclerView.getLayoutParams();
                //将高度赋值给列表
                layoutParams.height = i;
                recyclerView.setLayoutParams(layoutParams);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

通过CMD查看栈顶Activity

adb shell dumpsys activity | find “mFoc”