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