android源码里那些常用的Utils工具类

来源:互联网 发布:8080端口的作用 编辑:程序博客网 时间:2024/06/03 06:44

之前写项目遇到调用次数多的功能代码时,就需要写一个全局静态函数,并放在一个工具类中。不过后来发现,android系统本身提供了不少有用但不被发掘的工具类可供调用,使用系统类有助于缩减apk包大小,下面就和大家一起分析分析,如果有其他建议,可以在评论下面回复我哦。

1,android.database.DatabaseUtils

appendSelectionArgs:

函数功能:合并两个String数组,并返回合并后的数组。

public static String[] appendSelectionArgs(String[] originalValues, String[] newValues) {
        //。。。省去无数代码
        return result;
}



getSqlStatementType:

函数功能:判断sql语句的类型(SEL、INS、UPD、REP、DEL、ATT、COM、END、ROL、BEG、PRA、CRE、DRO、ALT、ANA、DET)

public static int getSqlStatementType(String sql) {
        //。。。省去无数代码
        return STATEMENT_OTHER;
}



cursor***ToContentValuesIfPresent(支持常用数据类型):

函数功能:将Cursor中指定的字段放到ContentValues数据中。

public static void cursor***ToContentValuesIfPresent(Cursor cursor, ContentValues values,
            String column) {
        final int index = cursor.getColumnIndex(column);
        if (index != -1 && !cursor.isNull(index)) {
            values.put(column, cursor.getFloat(index));
        }

}



queryIsEmpty:

函数功能:判断表是否为空

public static boolean queryIsEmpty(SQLiteDatabase db, String table) {
        long isEmpty = longForQuery(db, "select exists(select 1 from " + table + ")", null);
        return isEmpty == 0;
}




queryNumEntries:

函数功能:查询表中数据的行数

public static long queryNumEntries(SQLiteDatabase db, String table, String selection,
            String[] selectionArgs) {
        String s = (!TextUtils.isEmpty(selection)) ? " where " + selection : "";
        return longForQuery(db, "select count(*) from " + table + s,
                    selectionArgs);
    }




2,android.util.MonthDisplayHelper

绘制6行x7列日历时,帮助开发者计算行列值的日期和月份


getYear\getMonth\getWeekStartDay\getFirstDayOfMonth\getNumberOfDaysInMonth,这5个函数的功能分别是获取年份、月份、一周开始日(周一或周日)、本月开始日是周几、本月有多少天。


getDigitsForRow:

函数功能:获取日历每行的日期范围

public int[] getDigitsForRow(int row) {
        //。。。省去无数代码
        return result;
}


previousMonth:

函数功能: 在当前日期基础上回退一个月

public void previousMonth() {
        //。。。
}



nextMonth:

函数功能: 在当前日期基础上前进一个月

public void nextMonth() {
        //。。。
}


isWithinCurrentMonth:

函数功能:判断指定列行的日期是否在本月之内

public boolean isWithinCurrentMonth(int row, int column) {
//。。。
        return true;
    }



3, android.view.View.inflate函数:

函数功能:类似于LayoutInflater

public static View inflate(Context context, @LayoutRes int resource, ViewGroup root) {
        LayoutInflater factory = LayoutInflater.from(context);
        return factory.inflate(resource, root);
}


android.view.View.generateViewId:

函数功能:生成一个和aapt、R.id不冲突的id值,可用于setId时

public static int generateViewId() {
       //。。。省去无数代码
    }

0 0