这里是我的笔记,没有特别的信息给大家提供,大神勿喷。
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 …