android拨号器的实现
首先建立一个myphone的android项目
编写activity_main.xml 文件(页面布局文件)
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<EditText
android:id="@+id/numbers"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/number"
android:singleLine="true"
/>
<Button
android:id="@+id/btn_call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_call"
/>
</LinearLayout>
1.android:layout_width和android:layout_height的属性
android:layout_width="match_parent"
android:layout_height="match_parent"
有三个参数
1>.fill_parent android:layout_width="fill_parent" 在水平方向占满父元素,android:layout_height="fill_parent"在垂直方向上占满父元素
2>.match_parent 意思和fill_parent一样
3>.wrap_content 这个组件的android:text中内容长度而决定长短
2.android:id="@+id/numbers"的意思是给该组件创建一个Id,该id在gen文件下的R.java中,代码如下:
package com.wxw.phone;
public final class R {
public static final class attr {
}
public static final class dimen {
/** Default screen margins, per the Android Design guidelines.
Customize dimensions originally defined in res/values/dimens.xml (such as
screen margins) for sw720dp devices (e.g. 10" tablets) in landscape here.
*/
public static final int activity_horizontal_margin=0x7f040000;
public static final int activity_vertical_margin=0x7f040001;
}
public static final class drawable {
public static final int ic_launcher=0x7f020000;
}
public static final class id {
public static final int action_settings=0x7f080002;
public static final int btn_call=0x7f080001;
//这是numbers的id
public static final int numbers=0x7f080000;
}
public static final class layout {
public static final int activity_main=0x7f030000;
}
public static final class menu {
public static final int main=0x7f070000;
}
public static final class string {
public static final int action_settings=0x7f050001;
public static final int app_name=0x7f050000;
public static final int btn_call=0x7f050004;
public static final int hello_world=0x7f050002;
public static final int number=0x7f050003;
}
public static final class style {
/**
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
Base application theme for API 11+. This theme completely replaces
AppBaseTheme from res/values/styles.xml on API 11+ devices.
API 11 theme customizations can go here.
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.
API 14 theme customizations can go here.
*/
public static final int AppBaseTheme=0x7f060000;
/** Application theme.
All customizations that are NOT specific to a particular API-level can go here.
*/
public static final int AppTheme=0x7f060001;
}
}
这就给它一个id public static final int numbers=0x7f080000;
3.android:hint="@string/number"这句要产生一个在文本框中的提示内容如图
4.android:singleLine="true" EditText只显示一行,不能换行。
下面看MainActivity.java
package com.wxw.phone;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;
public class MainActivity extends Activity implements OnClickListener{
EditText numbers;
Button btn_call;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
numbers=(EditText) findViewById(R.id.numbers);
btn_call=(Button) findViewById(R.id.btn_call);
btn_call.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (TextUtils.isEmpty(numbers.getText())) {
Toast.makeText(this, "请输入电话好",Toast.LENGTH_SHORT).show();
}else{
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+numbers.getText().toString()));
startActivity(intent);
}
}
}
1. setContentView(R.layout.activity_main)这句代码是把activity_main这个加载到这个activity中,只有加载到activity才能显示在页面上,每一个页面就是一个activity。
2. numbers=(EditText) findViewById(R.id.numbers); 中findViewById是activity的方法里面参数是各个组件的id。然后根据各个组件的类型进行强制转换类型。
3. btn_call.setOnClickListener(this);这是给btn_call这个Button组件给了一个点击监听事件;setOnClickListener中的参数是必须是一个实现OnClickListener的类,而MainActivity恰好实现了OnClickListener这个类,实现这个类必须要实现里面的 public void onClick(View v) {}方法,也就是当点击Button时就会之间onClick方法,所以拨号的功能就在这个方法中实现
4. TextUtils.isEmpty(numbers.getText()) android提供了一个工具类TextUtils,其中的isEmpty就是判断参数是否为空。
5. 以下代码就是实现打电话功能
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+numbers.getText().toString()));
startActivity(intent);
首先Intent intent=new Intent()中文就是意图,你想要干什么,
intent.setAction(Intent.ACTION_CALL);就是一个动作干什么,给它的就是打电话
intent.setData(Uri.parse("tel:"+numbers.getText().toString()))就是既然打电话,望哪打给 它一个电话号
startActivity(intent);启动打电话的activity实现打电话功能
最后千万别忘了权限,对于android开发来说,打电话时必须要给一个打电话的权限,而这个权限是在AndroidManifest.xml中给的具体看代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wxw.phone"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.wxw.phone.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
就是这句 <uses-permission android:name="android.permission.CALL_PHONE"/> 给它一个打电话的权限
启动项目