標籤:tin private context click override eth 內部類 method 手機號
一、視頻筆記:
1.
當使用者點擊應用表徵圖——>
首先建立一個應用進程——>
再建立一個主線程——>
主線程中執行個體化Activity【註:OS會把應用有關的資訊(Context)存放進Activity】——>
調用onCreate()【註:OS調用,而不是使用者調用,一個生命週期內只被調用一次】
2.單位的使用
文字:sp
非文字:dp(=dip)
3.
開發軟體時在一個項目中首先將介面(.xml)的內容寫好。
二、進行實踐:
1.介面:
最終要實現的布局:
activity_main.xml中:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:orientation="vertical" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" > 5 6 <TextView 7 android:layout_width="match_parent" 8 android:layout_height="wrap_content" 9 android:text="@string/mobile" />10 11 <EditText 12 android:layout_width="match_parent"13 android:layout_height="wrap_content"14 android:id="@+id/mobile"15 />16 17 <Button 18 android:layout_width="wrap_content"19 android:layout_height="wrap_content"20 android:text="@string/button" 21 android:id="@+id/button"22 />23 24 </LinearLayout>
string.xml中(為了更加國際化):
<?xml version="1.0" encoding="utf-8"?><resources> <string name="app_name">電話撥號器</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string name="mobile">請輸入手機號</string> <string name="button">撥號</string></resources>
2.資訊清單檔中申請使用許可權:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.phone" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="21" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" 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><!--許可權:google為保護使用者的資訊安全和隱私資料,當使用到與此有關的資料時,必須申請相關許可權,在軟體安裝時會出現許可權提示 --> <uses-permission android:name="android.permission.CALL_PHONE"/></manifest>
3.代碼實現:
MainActivity.java:
package com.example.phone;import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;import android.widget.EditText;public class MainActivity extends Activity { private EditText mobileText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mobileText = (EditText)findViewById(R.id.mobile); Button button = (Button)this.findViewById(R.id.button); button.setOnClickListener(new ButtonClickListener());//或者使用匿名內部類:new實現了介面的類對象 } //內部類:大量使用,提交軟體的載入(到虛擬機器的)速度 private final class ButtonClickListener implements View.OnClickListener { @Override public void onClick(View v) {//輸入參數是當前被點擊的按鈕對象 // TODO Auto-generated method stub //每次點擊撥號,下面一句都會使用findViewById尋找一次資源id,造成耗時// EditText mobileText = (EditText)findViewById(R.id.mobile); String number = mobileText.getText().toString(); Intent intent = new Intent(); intent.setAction("android.intent.action.CALL");// intent.addCategory("android.intent.category.DEFAULT");//1代碼 intent.setData(Uri.parse("tel:"+number)); //將意圖傳給OS,由OS發現匹配的activity,進行啟用 startActivity(intent);//注意:方法內部會自動為Intent添加android.intent.category.DEFAULT,所以1代碼注釋不要 } }}
簡易的安卓撥號器