編輯:關於 Android 開發
各位好,小編又來分享知識了。上一篇筆記講解了 Android 工具與 SDK 的設定,今天則要帶大家認識更多 Android 基礎。先看看今天的學習目錄:
package="com.itheima.helloworld"

Android Debug Bridge(安卓除錯橋)是 Eclipse 與裝置之間的連線橋梁,adb 能為任何 Android 裝置建立連線,並取得目前連線裝置的所有資訊。
啟用 adb 後,可以方便地使用以下指令:
有些人可能會遇到問題:打開 CMD 視窗執行指令時卻執行不了,這時只要設定 adb 的環境變數即可解決。
開啟「控制台」→「系統與安全性」→「系統」→「進階設定」→「環境變數」→ 找到 Path 變數。

點擊「編輯」,先找到你的 SDK 中存放 adb.exe 的目錄:

取得該目錄路徑(示範路徑:F:\AndroidStudy\adt-bundle-windows-x86_64-20140702\sdk\platform-tools),將它複製到 Path 變數值中即可。
功能:使用者輸一個號碼,點擊撥打按鈕,啟動系統的電話應用把號碼撥出去。
1. 新建一個 Android 專案

2. 點擊 res 目錄下的 layout,打開 activity_main.xml 設定檔

3. 建立幾個控制項:文字編輯框與按鈕。
activity_main.xml:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="請輸入你要撥打的電話號碼" /> <EditText android:id="@+id/edit_Test" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_cell" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="打電話" /> </LinearLayout>
註 1:元件必須設定寬高,否則無法通過編譯:
android:layout_width="wrap_content" android:layout_height="wrap_content"
為了方便控制版面內容,這裡把 XML 中的 RelativeLayout 改成了 LinearLayout。
以下是控制項產生的效果圖:

註 2:若是手動建立的控制項,必須指定一個控制項 ID android:id="@+id/textView1",否則執行會出錯。
4. 編寫 Java 程式碼
點擊 src 下的 MainActivity.java,編寫如下程式碼:
package com.example.myfirstandroid;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//取得 btn_cell 按鈕物件
Button btn = (Button)this.findViewById(R.id.btn_cell);
//為該按鈕綁定監聽事件
btn.setOnClickListener(new MyListener());
}
//撰寫監聽內部類,實作 OnClickListener 監聽介面
class MyListener implements OnClickListener{
@Override
//重寫該介面的單擊事件方法
public void onClick(View v) {
//取得編輯文字物件
EditText editTest = (EditText)findViewById(R.id.edit_Test);
//取得編輯框中的內容
String number = editTest.getText().toString();
//取得系統內建的撥號功能,告訴系統要執行打電話操作
Intent intent = new Intent();
//將動作封裝到 Intent 中
intent.setAction(Intent.ACTION_CALL);
//設定撥打對象
intent.setData(Uri.parse("tel:" + number));
//將該動作交給系統執行
startActivity(intent);
}
}
}
以上簡單小程式的程式碼就寫完了,趕緊執行試試看吧!
總結:今天的 Android 知識點就分享完了,內容不多,主要是大致理解 Android 程式的基本結構。案例中也運用到內部類的定義——內部類在 Android 開發中十分常見,若對這部分不熟悉的讀者,也可以另行參考相關筆記。
百度地圖地址搜索與可視化定位
百度地圖地址搜索與可視化定位百度地圖可先定位目前位置,再透過 GeoCoder 將使用者輸入的城市與街道轉換為座標。畫面可由上方輸入列與下方 MapView 組成。Jav
Android ListView 管理範例:單擊、多選、刪除與刷新
範例目標本範例展示 ListView 的常見管理操作,包括單擊、編輯模式、多選、全選、刪除與資料刷新。適合需要批次管理列表項目的場景。資料與狀態管理列表資料可使用 Arr
Android 內存分析工具入門:Memory Monitor、Heap Viewer 與 Allocation Tracker
Heap Viewer、Memory Monitor 與 Allocation Tracker 都是用來「視覺化」你的 App 內存使用情况的輔助工具,三者各有側重,搭配
Android 系統架構全解析
Android 系統架構全解析Android 的本質,是在標準的 Linux 系統之上增加了名為 Dalvik 的 Java 虛擬機,並在這台虛擬機之上搭建了一套 Jav