這裏是我的筆記,沒有特別的信息給大家提供,大神勿噴。
1.1 目的
實現一個電話撥號器。
1.2 效果圖
1.3 步驟
1)創建一個帶有activity的應用
2)在layout設計佈局,如下(activity_main.xml)
<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=".MainActivity" >
<EditText
android:id="@+id/et_phoneNumber"
android:layout_width="match_parent"
android:layout_height="40dp" />
<Button
android:id="@+id/btn_Call"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignLeft="@+id/et_phoneNumber"
android:layout_below="@+id/et_phoneNumber"
android:text="@string/btn_call" />
</RelativeLayout>
3)在values/strings.xml中添加:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">電話撥號器</string>
<string name="action_settings">Settings</string>
<string name="btn_call">撥打</string>
</resources>
4)java代碼,綁定按鈕,監聽事件,單擊撥打處理,如下:
package com.example.telephonecaller;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener {
private Button btnCall;
private EditText et_phoneNumber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//綁定控件
btnCall = (Button)findViewById(R.id.btn_Call);
et_phoneNumber = (EditText)findViewById(R.id.et_phoneNumber);
//1.給按鈕設置點擊事件
btnCall.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View arg0) {
//2.拿到編輯框中的文本
String phoneNumber= et_phoneNumber.getText().toString();
//3.給這個號碼打電話(其實就是調用系統中另一個應用的Activity,可以用隱式Intent)
Intent intent =new Intent();
intent.setAction("android.intent.action.CALL");
intent.addCategory("android.intent.category.DEFAULT");
intent.setData(Uri.parse("tel:"+phoneNumber));//協議數據
startActivity(intent);
}
}
5)在AndroidManifest.xml中添加一句權限
...
<uses-permission android:name="android.permission.CALL_PHONE"/>
...
6)可以運行了run as …