android项目1:打电话
一、效果图
二、步骤
1、画好主界面
/call/res/layout/activity_main.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:orientation="vertical" >
6
7 <EditText
8 android:id="@+id/editText_phoneNumber"
9 android:layout_width="match_parent"
10 android:layout_height="wrap_content"
11 android:layout_weight="0.00"
12 android:ems="10" >
13
14 <requestFocus />
15 </EditText>
16
17 <Button
18 android:id="@+id/btn_call"
19 android:layout_width="match_parent"
20 android:layout_height="50dp"
21 android:layout_weight="0.00"
22 android:text="@string/btn_call" />
23
24 </LinearLayout>
2、编好代码
com.fry.call_1.MainActivity
1 package com.fry.call_1;
2
3
4
5
6
7 import android.app.Activity;
8 import android.content.Intent;
9 import android.net.Uri;
10 import android.os.Bundle;
11 import android.view.View;
12 import android.view.View.OnClickListener;
13 import android.widget.Button;
14 import android.widget.EditText;
15
16
17
18 public class MainActivity extends Activity{
19 private Button btn_call;//创建一个button对象
20 private EditText editText_phoneNumber;
21 protected void onCreate(Bundle savedInstanceState) {
22 super.onCreate(savedInstanceState);//父类操作
23 setContentView(R.layout.activity_main);//引入名为activity_main的界面
24 btn_call=(Button) findViewById(R.id.btn_call);//找id为btn_openActivity的button
25 editText_phoneNumber=(EditText) findViewById(R.id.editText_phoneNumber);
26
27 //1、给按钮设置点击事件
28 btn_call.setOnClickListener(new OnClickListener() {//设置button点击监听
29
30 @Override
31 public void onClick(View v) {//onclick事件
32 // TODO Auto-generated method stub
33 //2、拿到编辑框中的号码
34 String phoneNumber=editText_phoneNumber.getText().toString();
35 //3、给这个号码打电话
36
37 Intent intent=new Intent();//初始化intent
38 intent.setAction("android.intent.action.CALL");
39 intent.addCategory("android.intent.category.DEFAULT");
40 intent.setData(Uri.parse("tel:"+phoneNumber));
41 startActivity(intent);//打开activity
42 }
43 });
44 }
45 }
3、设置好权限
/call/AndroidManifest.xml
1 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
2 package="com.fry.call_1"
3 android:versionCode="1"
4 android:versionName="1.0" >
5
6 <uses-permission android:name="android.permission.CALL_PHONE" />
7
8 <uses-sdk
9 android:minSdkVersion="8"
10 android:targetSdkVersion="19" />
11
12 <application
13 android:allowBackup="true"
14 android:icon="@drawable/ic_launcher"
15 android:label="@string/app_name"
16 android:theme="@style/AppTheme" >
17 <activity
18 android:name="com.fry.call_1.MainActivity"
19 android:label="@string/app_name" >
20 <intent-filter>
21 <action android:name="android.intent.action.MAIN" />
22
23 <category android:name="android.intent.category.LAUNCHER" />
24 </intent-filter>
25 </activity>
26 <activity android:name="com.fry.call_1.Activity01" android:exported="true"></activity>
27 </application>
28
29 </manifest>