編輯:Android 開發實例
打電話和傳簡訊是手機最基礎的能力,因此在入門課程裡,「用程式撥號」幾乎是必做的練習。它牽涉到的觀念很典型:組出一個帶 data 的 Intent、宣告權限、再處理輸入為空的狀況。以下從建立專案開始走一遍。
新增一個 Android 專案,命名為 phoneCallDemo,套件結構維持預設即可。
畫面只需要三個元件:一段提示文字、一個電話輸入框、一顆撥號鈕。輸入框加上 android:phoneNumber="true",鍵盤就會自動切換成數字與號碼相關符號。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="請輸入手機號碼:" />
<EditText
android:id="@+id/et1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="phone" />
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="撥打電話" />
</LinearLayout>
直接撥號屬於敏感操作,必須在 AndroidManifest.xml 中宣告 CALL_PHONE:
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.android.test"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".PhoneCallDemo"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
補充一個現代開發必須知道的背景:CALL_PHONE 屬於危險權限等級,在 Android 6.0 以後除了宣告,還要在執行期透過 requestPermissions() 向使用者取得同意,否則會直接收到 SecurityException。
public class PhoneCallDemo extends Activity {
private Button bt;
private EditText et;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bt = (Button) findViewById(R.id.bt1);
et = (EditText) findViewById(R.id.et1);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String inputStr = et.getText().toString().trim();
if (inputStr.length() == 0) {
Toast.makeText(PhoneCallDemo.this,
"號碼不能為空", Toast.LENGTH_LONG).show();
return;
}
Intent phoneIntent = new Intent(Intent.ACTION_CALL,
Uri.parse("tel:" + inputStr));
startActivity(phoneIntent);
}
});
}
}
程式的骨架其實只有一行重點:Uri.parse("tel:" + 號碼)。這個 tel: 是系統約定好的 URI scheme,搭配 ACTION_CALL 會直接撥出;若改用 ACTION_DIAL,系統只會把號碼帶進撥號盤、由使用者按下通話鍵才撥出。
ACTION_CALL:一鍵撥出,需要 CALL_PHONE 權限,適合客服專線、緊急聯絡等明確情境。ACTION_DIAL:只帶號碼開啟撥號盤,不需敏感權限,也讓使用者有機會確認號碼是否正確,多數 App 用這個即可。無論選哪一種,都建議在組 Intent 前包上 try-catch 處理 ActivityNotFoundException,並對輸入內容做一次格式檢查——畢竟號碼會被送進系統的通話流程,錯誤字元沒有理由傳下去。
這個小練習示範的正是 Android 元件互動的標準套路:介面用 XML 描述、事件用 Listener 串接、跨應用能力用 Intent 表達、敏感能力用權限把關。把 tel: 換成 mailto:、geo: 或 https:,同一份程式碼就變成另一個功能了。
Android 登入畫面範例:帳號密碼與錯誤次數限制
登入畫面常用來驗證使用者身份。實作時通常需要兩個輸入欄位:使用者名稱與密碼,並加上登入按鈕。為了避免暴力嘗試,還可以限制錯誤次數。 建立輸入欄位 <EditText
Android 本地化實戰:用資源限定符自動切換語言
Android App 可能同時被台灣、日本、德國的使用者安裝使用。若介面文字硬寫在程式碼或版面檔中,就永遠只能呈現一種語言。本地化要做的,是把這些文字抽成資源,並依裝置
Android JSON 解析完整教學
JSON 是 JavaScript Object Notation 的縮寫,它是一種與語言無關的資料交換格式,憑藉著輕量、易讀的特性,成為 XML 之外相當受歡迎的替代方
高仿網易新聞頂部滑動標籤列的實作
這是網易新聞的主頁。底部用 TabHost 就能做出來,很基本;真正好看的是頂部那條會滑動的標籤列——點擊任一標籤,游標方塊會平滑滑到對應位置,同時切換下方內容。第一次