Android开发小知识点集锦

1.让EditText不自动获取焦点

症状:进入Activity就会自动弹出输入框
解决方案:
在该EditText的父级控件中任意找一个,设置成:

   android:focusable="true"
   android:focusableInTouchMode="true"

2.Uri转绝对路径

    private  String getFilePathByContentResolver(Context context, Uri uri) {
        if (null == uri) {
            return null;
        }
        Cursor c = context.getContentResolver().query(uri, null, null, null, null);
        String filePath  = null;
        if (null == c) {
            throw new IllegalArgumentException(
                    "Query on " + uri + " returns null result.");
        }
        try {
            if ((c.getCount() != 1) || !c.moveToFirst()) {
            } else {
                filePath = c.getString(
                        c.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA));
            }
        } finally {
            c.close();
        }
        return filePath;
    }

3.代码创建Drawable

   private LayerDrawable createLayerDrawable() {
        Drawable[] drawables = new Drawable[2];
        drawables[0] = createShapeDrawable(0xffff0000, 2, 0xffffffff);
        drawables[1] = createShapeDrawable(0xff00ff00, 2, 0xffffffff);
        return new LayerDrawable(drawables);
    }

    private GradientDrawable createShapeDrawable(int color, int radius, int strokeColor) {
        GradientDrawable drawable = new GradientDrawable();
        drawable.setCornerRadius(DensityUtil.dp2px(radius));//设置4个角的弧度
        drawable.setColor(color);// 设置颜色
        drawable.setStroke(DensityUtil.dp2px(0.5f), strokeColor);
        return drawable;
    }

你可能感兴趣的:(Android知识拓展)