博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android:Utils
阅读量:6533 次
发布时间:2019-06-24

本文共 4835 字,大约阅读时间需要 16 分钟。

hot3.png

一、NumberFormat

/** * Number formatter for integers */public static final NumberFormat FORMAT_INT = NumberFormat.getIntegerInstance();textView.setText(FORMAT_INT.format(number));
/** * 格式:##.#%,小数点保留一位,0的时候不显示小数点. */public static final DecimalFormat DF = new DecimalFormat("##.#%");

 

二、

/** * 计算页数 * * @param total 总数 * @param count 单页数目 */public static int calculatePage(int total, int count) {    if (total == 0 || count == 0) {        return 0;    }    if (total <= count) {        return 1;    }    int page = total / count;    if (total % count > 0) {        return page + 1;    } else {        return page;    }}

 

三、

/** * 纯数字(需要处理空字符串). */public static boolean isDigits(String str) {    return !TextUtils.isEmpty(str) && TextUtils.isDigitsOnly(str);}

 

/** * 绘制实心小圆点 * * @param view 宽高相等的View,默认效果是正方形 */private void drawPoint(View view) {    ShapeDrawable ovalDrawable = new ShapeDrawable(new OvalShape());    ovalDrawable.getPaint().setStyle(Paint.Style.FILL);    ovalDrawable.getPaint().setColor(Color.RED);    view.setBackground(ovalDrawable);}

 

四、屏幕参数

Display display = getWindowManager().getDefaultDisplay();Point size = new Point();display.getSize(size);int width = size.x;int height = size.y;

 

参考StackOverflow文章:

/** * 显示一组便签,根据标签宽度自动换行。 */public class TagsLayout extends ViewGroup {    private int mLineHeight;    public TagsLayout(Context context) {        super(context);    }    public TagsLayout(Context context, AttributeSet attrs) {        super(context, attrs);    }    /**     * 测量view及其内容来确定view的宽度和高度     */    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        final int width = MeasureSpec.getSize(widthMeasureSpec);        // The next line is WRONG!!! Doesn't take into account requested MeasureSpec mode!        int height = MeasureSpec.getSize(heightMeasureSpec) - getPaddingTop() - getPaddingBottom();        final int count = getChildCount();        int lineHeight = 0;        int xPos = getPaddingLeft();        int yPos = getPaddingTop();        for (int i = 0; i < count; i++) {            final View child = getChildAt(i);            if (child.getVisibility() != GONE) {                // 用来测量出view的大小                child.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST),                        MeasureSpec.makeMeasureSpec(height, MeasureSpec.UNSPECIFIED));                final int childW = child.getMeasuredWidth();                final LayoutParams lp = child.getLayoutParams();                lineHeight = Math.max(lineHeight, child.getMeasuredHeight() + lp.height);                if (xPos + childW > width) {                    xPos = getPaddingLeft();                    yPos += lineHeight;                }                xPos += childW + lp.width;            }        }        mLineHeight = lineHeight;        if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.UNSPECIFIED) {            height = yPos + lineHeight;        } else if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST) {            if (yPos + lineHeight < height) {                height = yPos + lineHeight;            }        }        // 存储测量得到的宽度和高度值        setMeasuredDimension(width, height);    }    @Override    protected LayoutParams generateDefaultLayoutParams() {        return new LayoutParams(1, 1); // default of 1px spacing    }    @Override    protected boolean checkLayoutParams(LayoutParams p) {        return (p instanceof LayoutParams);    }    /**     * @param changed view有新的尺寸或位置     * @param l       相对于父view的Left位置     * @param t       top     * @param r       right     * @param b       bottom     */    @Override    protected void onLayout(boolean changed, int l, int t, int r, int b) {        final int count = getChildCount();        final int width = r - l;        int xPos = getPaddingLeft();        int yPos = getPaddingTop();        for (int i = 0; i < count; i++) {            final View child = getChildAt(i);            if (child.getVisibility() != GONE) {                final int childW = child.getMeasuredWidth();                final int childH = child.getMeasuredHeight();                final LayoutParams lp = child.getLayoutParams();                if (xPos + childW > width) {                    xPos = getPaddingLeft();                    yPos += mLineHeight;                }                child.layout(xPos, yPos, xPos + childW, yPos + childH);                xPos += childW + lp.width;            }        }    }}
private boolean isEllipsized(TextView tv) {    boolean flag = false;    Layout l = tv.getLayout();    if (l != null) {        int lines = l.getLineCount();        if (lines > 0 && l.getEllipsisCount(lines - 1) > 0) {            flag = true;        }    }    return flag;}

四、

根据页面找到代码中对应的类:连上手机adb shell dumpsys activity | grep "mResumedActivity"

 

转载于:https://my.oschina.net/xsjayz/blog/398531

你可能感兴趣的文章
linux 学习(二)防火墙
查看>>
scala001
查看>>
android - SpannableString或SpannableStringBuilder以及string.xml文件中的整型和string型代替...
查看>>
自己选择的路,跪着走完吧——一个兔纸的话
查看>>
三端稳压器各个参数解释
查看>>
算法(Algorithms)第4版 练习 1.3.14
查看>>
virtual PC 打造IE6、IE7、IE8、IE9等多版本共存原版测试环境
查看>>
js面向对象1
查看>>
内部类
查看>>
高速数论变换(NTT)
查看>>
Springmvc的跳转方式
查看>>
加密原理介绍,代码实现DES、AES、RSA、Base64、MD5
查看>>
LINUX中常用操作命令
查看>>
python 获取进程pid号
查看>>
链表中插入一个节点的三种情况
查看>>
洛谷.4180.[模板]次小生成树Tree(Kruskal LCA 倍增)
查看>>
TCL函数“参数自动补全” 与 “help 信息显示”
查看>>
POJ1050To the Max
查看>>
汇编基础--标识符、标号、伪指令和指令
查看>>
Linux软中断、tasklet和工作队列
查看>>