Android学习3—电话拨号器
本測試主要實現了一個Android的撥打電話的功能
一:界面預覽
由圖中可以看出,這個Activity需要3個控件:TextView、EditText、Button
其實實現一個功能要經過幾個步驟:
1,Activity的設置(即界面布局)
由于本功能比較簡單,所以只有一個Activity,也即是使用Eclipse創建Android項目時默認創建的main_activity.xml
代碼如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
??? android:layout_width="match_parent"
??? android:layout_height="match_parent"
??? android:orientation="vertical" >
??? <TextView
??????? android:layout_width="fill_parent"
??????? android:layout_height="wrap_content"
??????? android:text="@string/mobile" />
???
??? <EditText
??????? android:id="@+id/main_activity_phonenumber"
??????? android:layout_width="fill_parent"
??????? android:layout_height="wrap_content" />
???
??? <Button
??????? android:id="@+id/main_activity_phonecaller"
??????? android:layout_width="fill_parent"
??????? android:layout_height="wrap_content"
??????? android:text="撥號"/>
</LinearLayout>
關于本段代碼的解釋:
??? 首先這個Activity的布局為線性布局:LinearLayout,在設置LinearLayout的時候別忘記了orientation屬性為vertical
??? 關于TextView控件,因為它在本例子中只提供了一個顯示的功能,所以不用設置其id屬性。每一個控件都必須要設置其兩個屬性:android:layout_width和android:layout_height,關于這兩個屬性,主要有兩個值:fill_parent和wrap_content。fill_parent表明此控件的寬度(或高度)屬性為填充滿其父窗口,wrap_content指明控件的高度(或寬度)為包圍其內容即可,也就是說,此時控件的大小將會根據控件中的內容而改變大小。
??? TextView的最后一個屬性:android:text用于顯示控件上的文字內容。
??? 到這里你可能會疑惑@string/mobile和@+id/main_activity_phonenumber各是什么意思,@string/表明反斜線后面的變量為字符串類型,其名稱為mobile(字符串的鍵值對在res/values/string.xml中配置)。而@+id/表示反斜線后面的變量為新添加的內容,此時在R.java文件中會自動生成變量名并賦值,所以接下來在MainActivity.java中可以通過R.id.+變量名來獲取此控件的ID。
???? 在控件的屬性中還有一個屬性非常重要,即android:layout_weight;關于它的詳細信息,我會在接下來的博客中講述,因為在這里我們并沒有用到。
?
2.MainActivity的代碼編寫(如果你的APP使用了多個Activity,即你需要自己創建一個Activity,這時不要忘了在AndroidMainifest.xml文件中添加Activity的注冊)
代碼如下:
package com.example.phonecaller;
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.widget.Button;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
??? private Button button;
??? private EditText editText;
???
??? @Override
??? protected void onCreate(Bundle savedInstanceState) {
??????? super.onCreate(savedInstanceState);
??????? setContentView(R.layout.activity_main);
???????
??????? button = (Button) findViewById(R.id.main_activity_phonecaller);
??????? editText = (EditText) findViewById(R.id.main_activity_phonenumber);
??????? button.setOnClickListener(new ButtonClickListener());
??? }
???
??? public final class ButtonClickListener implements View.OnClickListener
??? {
??????? @Override
??????? public void onClick(View v) {
??????????? // TODO Auto-generated method stub
??????????? String phoneNumber = editText.getText().toString().trim();
??????????? Intent phoneCallIntent = new Intent();
??????????? phoneCallIntent.setAction(Intent.ACTION_CALL);
??????????? phoneCallIntent.setData(Uri.parse("tel:" + phoneNumber));
??????????? startActivity(phoneCallIntent);
??????? }???
??? }
}
本段代碼的解釋:
本段代碼前面的導包的代碼不用納悶,在你使用Eclipse進行開發時,每當你用到某些包中的類時,按下Ctrl+1,Eclipse會自動補全需要加載的包(不用刻意地去學習Eclipse快捷鍵,在代碼編寫的過程中,你會逐漸熟悉并掌握這些快捷鍵的使用)
??? MainActivity繼承自ActionBarActivity,可能是由于我的Eclipse版本比較新的緣故,很多老版本的會在創建MainActivity時繼承自Activity,后來我看了一下代碼,ActionBarActivity繼承自Activity,所以此處你的MainActivity無論繼承哪一個都是可以的。繼承ActionBarActivity之后,實現其onCreate方法,在方法內部進行代碼功能的實現。
??? 首先通過findViewById()獲得控件的使用權,button.setOnClickListener(new ButtonClickListener());的意思是為這個button添加一個單擊事件,此方法的參數為一個對象,此對象實現了View.OnClickListener接口,并在ButtonClickListener類中實現了此接口的onClick()方法。
???? 在onClick方法中,首先獲取editText中的電話號碼(trim()方法為去掉字符串兩邊的空格),接下來創建一個Intent用于調用系統的打電話的功能,phoneCallIntent.setAction方法指明了這個Intent目的是ACTION_CALL,即打電話。setData方法向Intent傳遞撥號的電話號碼,由于setData方法接收的屬性為一個Uri,所以此處使用Uri的parse方法將字符串轉換為Uri。系統規定的傳遞給打電話的Intent的電話格式為tel:+phoneNumber。設置好Intent之后,調用startActivity()方法啟動此Intent。
注:考慮到代碼編寫的整潔性,所以我在給button控件添加單擊相應事件時沒有使用匿名內部類,而是重新編寫一個類來View.OnClickListener接口,我看了好多教程都采用匿名內部類的方式來編寫代碼,但看到一個采用這種方式編寫代碼的教程,認為這樣確實使代碼整潔了好多,故采用這種方式,至于讀者使用哪種方式,則自行選擇,每種方式都有其優缺點。
注意,由于調用系統的撥打電話的功能需要使用系統權限,所以要給此App添加一個用戶權限,在AndroidManifest.xml中添加如下代碼:
<uses-permission android:name="android.permission.CALL_PHONE"/>
最后運行程序:
?????????
轉載于:https://www.cnblogs.com/inghzhang/p/3870991.html
總結
以上是生活随笔為你收集整理的Android学习3—电话拨号器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Quick Cocos2dx 调试问题
- 下一篇: Windows 动态链接库DLL浅解