Layout--布局
常用的的就是線性布局:
<?xml version="1.0" encoding="utf-8"?>
<!--這是個線性布局-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<!--這個屬性規定它是縱向方式排列控制項-->
android:orientation="vertical"
<!--前面加了layout_的屬性,就是相對於父控制項的屬性,這裡是填充父控制項-->
android:layout_width="fill_parent"
<!--填充父控制項-->
android:layout_height="fill_parent"
<!--長寬都填充了父控制項,它是最頂層的,於是撐滿了整個螢幕-->
>
<!--我想讓兩個控制項平行排布,但是父控制項是縱向排布,那就再嵌套一個縱向排列的線性布局-->
<LinearLayout android:orientation="horizontal"
<!-- 和父類的一個意思 -->
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<!-- 一個文字框 -->
<TextView
<!--這個屬性,是寬度按照內容的寬度來定-->
android:layout_width="wrap_content"
android:layout_height="fill_parent"
<!-- 文本是通過資源擷取的 -->
android:text="@string/tvPhoneNum"
/>
<!--一個輸入框-->
<EditText android:id="@+id/etPhoneNum" <!-- id,定義它,就用這種格式 @+id/ -->
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/tvMessage"
/>
<EditText
android:id="@+id/etMessage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="top|left"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
<!-- 這個屬性規定其內容靠右 -->
android:gravity="right"
>
<Button
android:id="@+id/btnHistroy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<!-- 控制項的比重,當它為1,其它控制項沒有設定的時候,它優先填充剩餘的部分,
多於一個控制項時,根據設定了這個屬性的控制項的個數,然後與每個控制項中這個屬性得值運算的出每個控制項的填充比例
-->
android:layout_weight="1"
android:text="@string/btnHistroy"
/>
<Button
android:id="@+id/btnCall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnCall"
/>
<Button
android:id="@+id/btnSendMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnSendMessage"
/>
</LinearLayout>
</LinearLayout>
標籤中的屬性,也就兩種,一種是規定自己相對於自身內容的外觀,一種是相對於父控制項的外觀(以layout_開頭),也就是這個控制項的超系統與子系統(自己是首先想到了TRIZ 才這樣理解起來)。
編碼--擷取layout中的控制項對象
在layout中為某個控制項定義了id屬性
android:id="@+id/idName"
以 @+id 開頭,那麼R.java資源檔就會自動把它加入到id子類中,架構嘛,顯示在螢幕上的時候就已經建立了,你擷取不擷取,它都在哪裡,一個執行個體,在代碼中這樣擷取
findViewById(R.id.idName)
他返回的是個View,需要自己強轉成期望的類型。
Intent攜帶資料(打電話)轉向CALL(打電話) 以及 簡訊
這些操作需要許可權,要更改 AndroidManifest.xml 檔案
添加一下標籤,與<application>標籤同層級
<!-- 申請使用者授權 電話撥打-->
<uses-permission android:name="android.permission.CALL_PHONE"/>
<!-- 申請使用者授權 簡訊發送-->
<uses-permission android:name="android.permission.SEND_SMS"/>
---------------------------------------------------------
//擷取Intent
Intent i=new Intent();
//設定要轉向的Activity
i.setAction(Intent.ACTION_CALL);
//設定參數
i.setData(Uri.parse("tel:"+num));
//開始轉向
startActivity(i);
Intent 可以理解為意圖,它是聯絡每個活動(視圖活動、Activity)的紐帶
Intent.ACTION_CALL 就是電話那個視圖。。。。。
把電話號碼穿過去,電話號碼是個URI,格式是 "tel:電話號碼"
//簡訊管理器
SmsManager smMessage=SmsManager.getDefault();
//從控制項獲得簡訊內容
String message=etMessage.getText().toString();
if (message !=null && !"".equals(message)){
//都知道每條簡訊有字數限制,這個方法就是自動幫你拆分簡訊
ArrayList<String> messages=smMessage.divideMessage(message);
for(String sms:messages){
//發送拆分後的簡訊
smMessage.sendTextMessage(num, null, sms, null, null);
}
}
sendTextMessage有很多參數
void android.telephony.SmsManager.sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)
看看手冊,第一個目標地址,第二個源地址,空的話就是原生號,第三個簡訊內容,後兩個都可以為空白
I/O操作檔案
linux許可權很高,每個程式都有自己的uid,dalvik又是每個程式一個虛擬機器,uid根據包名來判斷
//獲得一個某檔案的輸出資料流
FileOutputStream fio=openFileOutput("myHistroy.txt", MODE_APPEND| MODE_PRIVATE);
//輸入資料流
openFileOutput("myHistroy.txt");
既然每個程式的uid都不同,那麼每個程式就相當於一個獨立的使用者了,它們都有自己的使用者目錄,儲存自己的資料,位於
/data/data/程式包名
按著上面的方法建立的檔案,屬於這個檔案夾下面得東西
/data/data/程式包名/files/檔案