Android基础第二天

public void addTime(){
	SQLiteDatabase database = helper.getWritableDatabase();
	for (int i = 0; i < 10000; i++) {
		ContentValues values1 = new ContentValues();
		values1.put("_id", i);
		values1.put("name", "xxc"+i);
		values1.put("age", i);
		database.insert("test1", "_id", values1);
	}
}

不开事务向数据库插入一万条数据,用时21911毫秒


public void addTime1(){
	SQLiteDatabase database = helper.getWritableDatabase();
	try {
		database.beginTransaction();
		for (int i = 0; i < 10000; i++) {
			ContentValues values1 = new ContentValues();
			values1.put("_id", i);
			values1.put("name", "xxc"+i);
			values1.put("age", i);
			database.insert("test1", "_id", values1);
			database.setTransactionSuccessful();
		}
	} catch (Exception e) {
		e.printStackTrace();
	}finally{
		database.endTransaction();
	}
}
开启事务向数据库插入一万条数据,用时3347毫秒


setTransactionSuccessful:调用该方法设置事务成功;否则endTransaction方法将回滚事务

endTransaction:有事务的标志决定是提交事务还是回滚事务


结论就是:

android在批量操作数据的时候开事务要比不开事务效率要高!



android:visibility属性

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical">
	<Button 
	    android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:text="按钮一"/>
	<Button 
	    android:layout_weight="1"
	    android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:text="按钮二"/>
	<Button 
	    android:visibility="visible"   默认显示
	    android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:text="按钮三"/>
</LinearLayout>



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical">
	<Button 
	    android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:text="按钮一"/>
	<Button 
	    android:layout_weight="1"
	    android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:text="按钮二"/>
	<Button 
	    android:visibility="invisible"   不显示,但是位置保留
	    android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:text="按钮三"/>
</LinearLayout>



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical">
	<Button 
	    android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:text="按钮一"/>
	<Button 
	    android:layout_weight="1"
	    android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:text="按钮二"/>
	<Button 
	    android:visibility="gone"   不显示,位置也不保留
	    android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:text="按钮三"/>
</LinearLayout>



当LinearLayout布局的时候将组件放在屏幕左边,要使用layout_gravity属性(此时是没有layout_alignParentRight属性的)。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="右边" />
</LinearLayout>


当布局是RelativeLayout的时候,将组件放在屏幕左边要使用layout_alignParentRight属性(此时是没有layout_gravity属性的)。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
	<Button 
	    android:layout_alignParentRight="true"
	    android:layout_width="wrap_content"
    	android:layout_height="wrap_content"/>
</RelativeLayout>






练习:


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <Button 
        android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:text="上面"/>
    <Button
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true" 
        android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:text="下面"/>
    <Button
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:text="左面"/>
    <Button
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:text="右面"/>
    <Button
        android:id="@+id/center_BT"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:text="中面"/>
    <Button
        android:layout_alignBaseline="@id/center_BT"
        android:layout_toLeftOf="@id/center_BT"
        android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:text="中左"/>
    <Button
        android:layout_alignBaseline="@id/center_BT"
        android:layout_toRightOf="@id/center_BT"
        android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:text="中右"/>
    <Button
        android:layout_centerHorizontal="true"
        android:layout_above="@id/center_BT"
        android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:text="中上"/>
    <Button
        android:layout_centerHorizontal="true"
        android:layout_below="@id/center_BT"
        android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:text="中下"/>
</RelativeLayout>

getFilesDir():获取的文件夹路径是   /data/data/包名/files

getCacheDir():获取的文件夹路径是  /data/data/包名/cache

openFileOutput("a.txt",0); 获取的文件路径是  /data/data/包名/files/a.txt

SharedPreferences存储路径:  /data/data/包名/shared_prefs/

在Android4.0之前读SD卡是不需要权限的,4.0之后则需要权限

//获取的文件夹路径是  SD卡的路径
File sdFile = Environment.getExternalStorageDirectory();
if(!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){//判断SD卡状态是否可用
	Toast.makeText(getApplicationContext(), "SD卡不可用", Toast.LENGTH_SHORT).show();
	return;
}



获取指定目录总空间和已用空间大小:

package com.xxc.util;

import java.io.File;

import android.content.Context;
import android.os.StatFs;
import android.text.format.Formatter;

public class MemoryUtil {
	/**
	 * 获取指定文件夹总空间的大小和可用空间大小
	 * @param path 指定文件夹File对象
	 * @return
	 */
	public static String getMemoryInfo(Context context,File path){
		//获取一个磁盘状态对象
		StatFs stat = new StatFs(path.getPath());
		
		long blockSize = stat.getBlockSize();				//获取一个扇区的大小
		long totalBlocks = stat.getBlockCount();			//获取扇区个数
		long availableBlocks = stat.getAvailableBlocks();	//获取可用扇区数量
		
		//总空间
		String totalMemory = Formatter.formatFileSize(context, totalBlocks * blockSize);
		//可用空间
		String availableMemory = Formatter.formatFileSize(context,availableBlocks * blockSize);
		
		StringBuffer sb = new StringBuffer();
		sb.append("总空间:");
		sb.append(totalMemory);
		sb.append("\n可用空间:");
		sb.append(availableMemory);
		
		return sb.toString();
	}
}



评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值