標籤:必須 instance 壓力測試 sele dex 集合 move 名稱 系統
相對PC蛋蛋源碼出租 dsluntan.com Q:3393756370 VX:17061863513布局 RelativeLayout
組件預設靠左對齊、頂部對齊
設定組件在指定組件的右邊
android:layout_toRightOf="@id/tv1"
設定在指定組件的下邊
android:layout_below="@id/tv1"
設定靠右對齊父元素
android:layout_alignParentRight="true"
設定與指定組件靠右對齊
android:layout_alignRight="@id/tv1"
線性布局LinearLayout
指定各個節點的排列方向
設定靠右對齊
android:layout_gravity="right"
當豎直布局時,只能左靠右對齊和水平置中,頂部底部對齊豎直置中無效
當水平布局時,只能頂部底部對齊和豎直置中
使用match_parent時注意不要把其他組件頂出去
線性布局非常重要的一個屬性:權重
android:layout_weight="1"
權重:按比例分配螢幕的剩餘寬度或者高度
幀布局FrameLayout
預設組件都是靠左對齊和頂部對齊,每個組件相當於一個div
可以設定上下左靠右對齊,水平豎直置中,設定方式與線性布局一樣
android:layout_gravity="bottom"
不能相對於其他組件布局
表格版面配置TableLayout
每個節點是一行,它的每個子節點是一列
表格版面配置中的節點可以不設定寬高,因為設定了也無效
根節點的子節點寬為匹配父元素,高為包裹內容
節點的子節點寬為包裹內容,高為包裹內容
以上預設屬性無法修改
根節點中可以設定以下屬性,表示讓第1列展開填滿螢幕寬度的剩餘空間
android:stretchColumns="1"
絕對布局AbsoluteLayout
直接指定組件的x、y座標
android:layout_x="144dp"
android:layout_y="154dp"
注意:直接複製項目需要改動的地方:項目名字、應用程式套件名、R檔案重新導包
路徑API說明
getFilesDir()得到的file對象的路徑是data/data/應用程式套件名/files
存放在這個路徑下的檔案,只要你不刪,它就一直在
getCacheDir()得到的file對象的路徑是data/data應用程式套件名/cache
存放在這個路徑下的檔案,當記憶體不足時,有可能被刪除
系統管理應用介面的清除緩衝,會清除cache檔案夾下的東西,清除資料,會清除整個包名目錄下的東西
sd卡的路徑
2.2之前,sd卡路徑:sdcard
4.3之前,sd卡路徑:mnt/sdcard
4.3開始,sd卡路徑:storage/sdcard
最簡單的開啟sd卡的方式
File file = new File("sdcard/test.txt");
使用api獲得sd卡的真實路徑,部分手機品牌會更改sd卡的路徑
Environment.getExternalStorageDirectory()
判斷sd卡是否準備就緒
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
擷取SD容量
import java.io.File;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.text.format.Formatter;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView tv;br/>@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv);
if(existSDCard()){
String available = formatSize(getAvailable());
String allSize = this.formatSize(getAllSize());
tv.setText("總空間:"+allSize+" 可用空間"+available);
}
}
//擷取可用容量private long getAvailable(){ File path = Environment.getExternalStorageDirectory(); StatFs sf = new StatFs(path.getPath()); //擷取單個資料區塊的大小(Byte) long blockSize = sf.getBlockSizeLong(); //擷取所有資料區塊數 long availbleBlocks = sf.getAvailableBlocksLong(); return blockSize* availbleBlocks;}//擷取總容量private long getAllSize(){ File path = Environment.getExternalStorageDirectory(); StatFs sf = new StatFs(path.getPath()); //擷取單個資料區塊的大小(Byte) long blockSize = sf.getBlockSizeLong(); //擷取所有資料區塊數 long allBlocks = sf.getBlockCountLong(); return blockSize*allBlocks;}//格式化函數private String formatSize(long size) { return Formatter.formatFileSize(this, size);}//是否掛載SD卡private boolean existSDCard() { if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { return true; } else return false;}
}
注意:在使用getBlockCountLong()等API的時候主要SDK的版本,SDK17是不支援getBlockCountLong()的,低版本的SDK仍然使用getBlockCount()等不帶Long的
這裡寫圖片描述
測試
測試分類
黑箱測試
測試邏輯業務
白盒測試
測試邏輯方法
根據測試粒度
方法測試:function test
單元測試:unit test
整合測試:integration test
系統測試:system test
根據測試暴力程度
煙霧測試 (Smoke Test):smoke test(例如monkey 1000:在螢幕上隨機點擊1000次)
壓力測試:pressure test
Android中的單元測試
首先要使用一個類繼承自AndroidTestCast:
import android.test.AndroidTestCase;
public class Test extends AndroidTestCase {
public void test(){
//TODO 測試的代碼或者方法
}
}
並且在中做出如下配置:
<instrumentation android:name="android.test.InstrumentationTestRunner"android:allowBackup="true"
android:icon="@drawable/ic_launcher"
br/>android:targetPackage="要測試的應用程式套件名">
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:theme="@style/AppTheme" >
SQLite輕量級關係型資料庫建立資料庫需要使用的api:SQLiteOpenHelper資料庫被建立時會調用:onCreate方法資料庫升級時會調用:onUpgrade方法import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;public class MyOpenHelper extends SQLiteOpenHelper {public MyOpenHelper(Context context) { //參數說明:上下文、資料庫名稱、遊標工廠(預設使用NULL)、版本(用於升級) super(context, "test.db", null, 1);}@Overridepublic void onCreate(SQLiteDatabase db) { //建立表 db.execSQL("create table person(_id integer primary key autoincrement, name char(10), phone char(20), salary integer(10))");}//資料庫升級的時候調用@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { System.out.println("資料庫升級...");}}建立資料庫//建立OpenHelper對象MyOpenHelper oh = new MyOpenHelper(getContext(), "test.db", null, 1);//獲得資料庫物件,如果資料庫不存在,先建立資料庫,後獲得,如果存在,則直接獲得SQLiteDatabase db = oh.getWritableDatabase();getWritableDatabase():開啟可讀寫的資料庫getReadableDatabase():在磁碟空間不足時開啟唯讀資料庫,否則開啟可讀寫資料庫資料庫的增刪改查資料庫的增刪改查可以自己使用SQL語句,然後db.execSQL("SQL語句")即可完成!注意:setup主要實現測試前的初始化工作,而teardown則主要實現測試完成後的記憶體回收等工作。@Overrideprotected void setUp() throws Exception { super.setUp(); //擷取虛擬內容物件 oh = new MyOpenHelper(getContext(), "test.db", null, 1);}@Overrideprotected void tearDown() throws Exception { super.tearDown(); //關閉資料庫 db.close();}使用API實現增刪改查//使用遊標來逐個訪問每條資料public void select(){ Cursor cursor = db.rawQuery("select * from 表名", null); //把指標移動至下一行 while(cursor.moveToNext()){ //先通過列名,擷取列索引,然後再擷取該列的內容 String str = cursor.getString(cursor.getColumnIndex("欄位名1")); String str2 = cursor.getString(cursor.getColumnIndex("欄位名2")); int str3 = cursor.getInt(cursor.getColumnIndex("欄位名3")); System.out.println(str+str2+str3); }}public void insertApi(){ ContentValues values = new ContentValues(); values.put("欄位1", "value"); values.put("欄位2", "value"); values.put("欄位3", "value"); //傳回值-1,插入失敗 long l = db.insert("表名", null, values);}public void deleteApi(){ //因為後面要傳入的是對象數組 int i = db.delete("表名", "_id = ?", new String[]{"4"});}public void updateApi(){ ContentValues values = new ContentValues(); values.put("欄位名", "value"); int i = db.update("表名", values, "_id = ?", new String[]{"1"});}事務保證多條SQL語句要麼同時成功,要麼同時失敗,失敗的話會發生復原!public void transaction(){ try{ //開啟事務 db.beginTransaction(); //TODO 修改表資料... //設定事務執行成功,提交時如果這行代碼沒有執行過,就會復原 db.setTransactionSuccessful(); } catch (Exception e) { //這裡的異常必須要捕獲 e.printStackTrace(); } finally{ //關閉事務,提交資料 db.endTransaction(); }}ListView用於顯示列表MVC結構M:model模型層,要顯示的資料 ————people集合V:view視圖層,使用者看到的介面 ————ListViewc:control控制層,操作資料如何顯示 ————adapter對象每一個條目都是一個View對象BaseAdapter必須實現的兩個方法// 適配器類class MyAdapter extends BaseAdapter { // 系統調用:擷取模型層資料的數量 @Override public int getCount() { return list.size(); } // 系統調用:擷取要顯示至ListView的View對象 // convertView:系統之前緩衝的條目 @Override public View getView(int position, View convertView, ViewGroup parent) { People p = list.get(position); View view = null; if (convertView == null) { // 把布局檔案填充為view對象(看源碼,這就是三種方式) // LayoutInflater layIn = LayoutInflater.from(MainActivity.this); // LayoutInflater layIn = (LayoutInflater)getSystemService("layout_inflater"); // view = layIn.inflate(R.layout.item_listview,null); view = View.inflate(getApplicationContext(),R.layout.item_listview, null); } else { view = convertView; TextView tv_name = (TextView) view.findViewById(R.id.tv_name); TextView tv_phone = (TextView) view.findViewById(R.id.tv_phone); TextView tv_sa = (TextView) view.findViewById(R.id.tv_sa); tv_name.setText(p.getName()); tv_phone.setText(p.getPhone()); tv_sa.setText(p.getSalary()); } return view; }}螢幕上能顯示多少個條目,getView方法就會被調用多少次,螢幕向下滑動時,getView會繼續被調用,建立更多的View對象顯示至螢幕條目的緩衝當條目划出螢幕時,系統會把該條目緩衝至記憶體,當該條目再次進入螢幕,系統在重新調用getView時會把緩衝的條目作為convertView參數傳入,但是傳入的條目不一定是之前被緩衝的該條目,即系統有可能在調用getView方法擷取第一個條目時,傳入任意一個條目的緩衝!ArrayAdapter在條目中顯示一個字串String[] objects = new String[]{"AAA","BBB","CCC"};ListView lv = (ListView) findViewById(R.id.lv);//arg1:指定要填充的布局檔案//arg2:指定文本顯示至哪一個文字框內lv.setAdapter(new ArrayAdapter(this, R.layout.item_array, R.id.tv_name, objects));SimpleAdapter可在條目中顯示多種資料要顯示的資料封裝在List中,集合的每一個元素存放的是一個條目會顯示的資料,因為可能會有多種資料,而集合的泛型只能指定一種資料,所以把資料先存放在Map中,在把Map放入List中protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView)findViewById(R.id.lv); //把每個條目需要處理的所有資料封裝至map中,在把map封裝至list中 //這樣就保證了list每個元素都包含了一個條目需要的所有資料 List> data = new ArrayList>(); Map m1 = new HashMap(); m1.put("name","AAA"); m1.put("Photo",R.drawable.a); data.add(m1); Map m2 = new HashMap(); m2.put("name","BBB"); m2.put("Photo",R.drawable.b); data.add(m2); Map m3 = new HashMap(); m3.put("name","CCC"); m3.put("Photo",R.drawable.c); data.add(m3); lv.setAdapter(new SimpleAdapter(MainActivity.this, data, R.layout.item_listview, new String[]{"name","Photo"} , new int[]{R.id.name, R.id.iv}));
布局、SD路徑、單元測試、SQLitePC蛋蛋源碼出租與ListView