一.搭建開發環境
1.所需資源
JDK6以上
Eclipse3.6以上
SDK17, 2.3.3
ADT17
2.安裝注意事項
不要使用中文路徑
若是模擬器默認路徑包含中文, 能夠設置android_sdk_home環境變量解決。效果以下:java
二.撥打電話
1.步驟
在Button節點中添加onClick屬性, 指定一個方法名
在Activity中定義一個public void 方法名 (View view)
獲取文本框中的號碼
建立意圖, 設置動做, 設置數據
使用意圖開啓Activity
2.注意
必須聲明權限: android.permission.CALL_PHONE, 不然撥打電話時會拋出異常
聲明權限後, 安裝軟件時會有提示android
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.shellway.phone.MainActivity" > <TextView android:id="@+id/tv_phone" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/input_num" /> <EditText android:inputType="phone" android:id="@+id/et_phone" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/tv_phone" android:ems="10" > <requestFocus /> </EditText> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/et_phone" android:text="@string/call" /> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Phone</string> <string name="input_num">請輸入號碼</string> <string name="action_settings">Settings</string> <string name="call">呼叫此號碼</string> </resources>
package com.shellway.phone; import android.support.v7.app.ActionBarActivity; 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.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class MainActivity extends ActionBarActivity implements OnClickListener { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //經過findViewById()方法獲取按鈕對象 Button button = (Button) findViewById(R.id.button1); //給這個按鈕添加一個監聽對象 button.setOnClickListener(this);//由於自己實現了監聽接口,因此用this } public void onClick(View v) { EditText editText = (EditText) findViewById(R.id.et_phone); String num = editText.getText().toString(); //建立一個意圖 Intent intent = new Intent(); intent.setAction(Intent.ACTION_CALL);//給這個意圖添加動做 intent.setData(Uri.parse("tel:" + num));//給這個意圖添加數據 startActivity(intent);//新開一個窗體執行這個意圖 } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.shellway.phone" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <uses-permission android:name="android.permission.CALL_PHONE"/> <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> </manifest>
三.發送短信
1.步驟
在onClick方法中獲取號碼和內容
獲取短信管理器
將內容分段
發送短信
彈出Toast提示
2.注意
短信和電話都屬於付費功能, 須要聲明權限: android.permission.SEND_SMS
shell
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.shellway.sms.MainActivity" > <TextView android:id="@+id/TextView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/number" /> <EditText android:id="@+id/numberET" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="phone" android:layout_below="@id/TextView1" /> <TextView android:id="@+id/TextView2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/content" android:layout_below="@id/numberET" /> <EditText android:id="@+id/contentET" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="textMultiLine" android:lines="3" android:layout_below="@id/TextView2" /> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/send" android:layout_below="@id/contentET" /> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">SMS</string> <string name="number">電話號碼</string> <string name="content">短信內容</string> <string name="send">發送短信</string> <string name="action_settings">Settings</string> </resources>
package com.shellway.sms; import java.util.ArrayList; import android.support.v7.app.ActionBarActivity; import android.telephony.SmsManager; import android.content.Context; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends ActionBarActivity implements OnClickListener { private EditText num; private EditText content; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); num = (EditText) findViewById(R.id.numberET); content = (EditText) findViewById(R.id.contentET); Button button = (Button) findViewById(R.id.btn); button.setOnClickListener(this); } public void onClick(View v) { String sms_number = num.getText().toString(); String sms_content = content.getText().toString(); SmsManager smsManager = SmsManager.getDefault(); // 將短信內容分割 ArrayList<String> list = smsManager.divideMessage(sms_content); for (String s : list) smsManager.sendTextMessage(sms_number, null, s, null, null); // 獲取上下文 Context context = getApplicationContext(); CharSequence text = "發送成功";// 通知內容 int duration = Toast.LENGTH_SHORT;// 彈出通知持續的時長 Toast toast = Toast.makeText(context, text, duration); toast.show(); /* //發送成功通知,這裏等價於上面註釋掉的5行內容 Toast.makeText(getApplicationContext(), "發送成功", Toast.LENGTH_SHORT).show();*/ } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.shellway.sms" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <uses-permission android:name="android.permission.SEND_SMS"/> <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> </manifest>
四.佈局
1.LinearLayout
分爲水平和垂直兩種, 能夠嵌套使用app
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/name" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/name" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/name" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/name" /> </LinearLayout> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/name" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/name" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/name" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/name" /> </LinearLayout>
2.FrameLayout
後寫的覆蓋先寫的
ide
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:src="@drawable/transformers" android:layout_width="match_parent" android:layout_height="match_parent" /> <ImageView android:src="@drawable/play" android:layout_width="100dp" android:layout_height="100dp" android:layout_gravity="center" /> </FrameLayout>
3.TableLayout
相似與HTML的<table>標籤佈局
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:stretchColumns="1" > <!--一、 stretchColumns:指定元素伸縮,從0開始 。 gravity:指定元素的重心位置 二、若要平均分配寬度則用:layout_width="0dp"和layout_weight="1"組合。 它也能夠用來佔滿剩下的位置,前面的1。1 1.2都爲100dp 那麼1.3能夠用它們來佔滿剩下的位置 --> <TableRow> <TextView android:layout_width="0dp" android:layout_weight="1" android:padding="10dp" android:gravity="center" android:text="1.1" /> <TextView android:layout_width="0dp" android:layout_weight="1" android:padding="10dp" android:gravity="center" android:text="1.2" /> <TextView android:layout_width="0dp" android:layout_weight="1" android:padding="10dp" android:gravity="center" android:text="1.3" /> </TableRow> <TableRow> <TextView android:padding="10dp" android:text="2.1" /> <TextView android:padding="10dp" android:gravity="center" android:text="2.2" /> <TextView android:padding="10dp" android:text="2.3" /> </TableRow> </TableLayout>
4.RelativeLayout
相對佈局, 元素位置相對與其餘元素定位this
package com.shellway.layout; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.relativelayout); } /** * 由於在XML中給按鈕設置了onClick屬性,還沒實現按鈕監聽並響應事件時, * 爲了在模擬器上點擊按鈕不讓程序崩潰先給它加個個空的onClick方法 * @param view */ public void onClick(View view){ } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" > <TextView android:id="@+id/TextView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="15sp" android:text="手機號碼:" /> <EditText android:id="@+id/EditText1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toRightOf="@id/TextView1" android:layout_marginLeft="25dp" android:inputType="phone" /> <TextView android:id="@+id/TextView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/EditText1" android:textSize="15sp" android:text="短信內容:" /> <EditText android:id="@+id/EditText2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignTop="@id/TextView2" android:layout_toRightOf="@id/TextView2" android:layout_marginLeft="25dp" android:inputType="textMultiLine" android:lines="3" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/EditText2" android:layout_alignParentRight="true" android:onClick="onClick" android:text="發送短信" /> </RelativeLayout>
5.AbsoluteLayout
用座標x,y定位元素的絕對位置spa
<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="41dp" android:layout_y="26dp" android:text="Button" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="43dp" android:layout_y="124dp" android:text="Button" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="174dp" android:layout_y="35dp" android:text="Button" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="114dp" android:layout_y="202dp" android:text="Button" /> </AbsoluteLayout>