Android快速入門

來源:互聯網
上載者:User

項目源碼下載

https://github.com/Wang-Jun-Chao/AndroidProjects

Android項目的目錄結構

Activity:應用被開啟時顯示的介面

src:項目代碼

R.java:項目中所有資源檔的資源id

Android.jar:Android的jar包,匯入此包方可使用Android的api

libs:匯入第三方jar包

assets:存放資源檔,比方說mp3、視頻檔案

bin:存放編譯打包後的檔案

res:存放資源檔,存放在此檔案夾下的所有資源檔都會產生資源id

drawable:存放圖片資源

layout:存放布局檔案,把布局檔案通過資源id指定給activity,介面就會顯示出該布局檔案定義的布局

menu:定義菜單的樣式

Strings.xml:存放字串資源,每個資源都會有一個資源id

Android的設定檔(資訊清單檔)

指定應用的包名

package="com.itheima.helloworld"

data/data/com.itheima.helloworld(上面代碼指定的包名)

應用產生的檔案都會存放在此路徑下

Android的四大組件在使用前全部需要在資訊清單檔中配置

的配置對整個應用生效

的配置對該activity生效

DDMS

Dalvik debug monitor service

Dalvik調試監控服務

常用的adb指令

Android debug bridge:安卓調試橋

adb start-server:啟動adb進程

adb kill-server:殺死adb進程

adb devices:查看當前與開發環境串連的裝置,此命令也可以啟動adb進程

adb install XXX.apk:往模擬器安裝apk

adb uninstall 包名:刪除模擬器中的應用

adb shell:進入linux命令列

ps:查看運行進程

ls:查看目前的目錄下的檔案結構

netstat -ano:查看佔用連接埠的進程

電話撥號器

功能:使用者輸入一個號碼,點擊撥打按鈕,啟動系統打電話的應用把號碼撥打出去

1. 定義布局

組件必須設定寬高,否則不能通過編譯

android:layout_width="wrap_content"android:layout_height="wrap_content"

如果要在java代碼中操作某個組件,則組件需要設定id,這樣才能在代碼中通過id拿到這個組件

android:id="@+id/et_phone"

2. 給按鈕設定點擊偵聽

給按鈕設定偵聽

 //通過id拿到按鈕對象Button bt_call = (Button) findViewById(R.id.bt_call);//給按鈕設定點擊bt_call.setOnClickListener(new MyListener());

3. 得到使用者輸入的號碼

    //得到使用者輸入的號碼,先拿到輸入框組件        EditText et_phone = (EditText) findViewById(R.id.et_phone);        String phone = et_phone.getText().toString();

4. 把號碼打出去

Android系統中基於動作機制,來調用系統的應用,你告訴系統你想做什麼動作,系統就會把能做這個動作的應用給你,如果沒有這個應用,會拋異常

設定動作,通過意圖告知系統

//把號碼打出去    //先建立一個意圖對象    Intent intent = new Intent();    //設定動作,打電話    intent.setAction(Intent.ACTION_CALL);    intent.setData(Uri.parse("tel:" + phone));    //把意圖告訴系統    startActivity(intent);

添加許可權

<uses-permission android:name="android.permission.CALL_PHONE"/>

點擊事件的四種寫法

第一種

定義一個MyListener實現onClickListener介面

Button bt1 = (Button) findViewById(R.id.bt1);bt1.setOnClickListener(new MyListener());

第二種

定義一個匿名內部類實現onClickListener介面

Button bt2 = (Button) findViewById(R.id.bt2);bt2.setOnClickListener(new OnClickListener() {    @Override    public void onClick(View v) {        System.out.println("第二種");    }});

第三種

讓當前activity實現onClickListener介面

Button bt3 = (Button) findViewById(R.id.bt3);bt3.setOnClickListener(this);

第四種

給Button節點設定onClick屬性,

 android:onClick="click"

然後在activity中定義跟該屬性值同名的方法

public void click(View v){    System.out.println("第四種");}

簡訊發送器

功能:使用者輸入號碼和簡訊內容,點擊發送按鈕,調用簡訊api把簡訊發送給指定號碼

1. 定義布局

輸入框的提示

android:hint="請輸入號碼"  

2. 完成點擊事件

先給Button組件設定onClick屬性

onClick=”send”

在Activity中定義此方法

public void send(View v){}

3. 擷取到使用者輸入的號碼和內容

    EditText et_phone = (EditText) findViewById(R.id.et_phone);    EditText et_content = (EditText) findViewById(R.id.et_content);    String phone = et_phone.getText().toString();    String content = et_content.getText().toString();

4. 調用傳送簡訊的api

    //調用傳送簡訊的api    SmsManager sm = SmsManager.getDefault();    //傳送簡訊    sm.sendTextMessage(phone, null, content, null, null);

* 添加許可權

     <uses-permission android:name="android.permission.SEND_SMS"/>

* 如果簡訊過長,需要拆分

    List<String> smss = sm.divideMessage(content);

更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.