一、android的电话拨号器

1、项目结构:一、android的电话拨号器_第1张图片

效果图如下:一、android的电话拨号器_第2张图片

2、AndroidManifest.xml的配置文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cn.huangjie.phone"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".MainActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
	<!-- android在一些个人隐私方面对做权限的申请,如果用户在安装软件的时候该软件
	会涉及到拨号功能,那么在安装的时候android系统会提示用户会用到此功能,提示用
	户要不要安装该软件,但是在开发环境中,是部署到模拟器上面的,是不会进行提示的 -->
    <uses-permission android:name="android.permission.CALL_PHONE"/>
</manifest>

3、main.xml布局管理文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/phone" />
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/mobile" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button" 
        android:id="@+id/button"/>
</LinearLayout>

4、strings.xml数值管理文件

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Hello World, MainActivity!</string>
    <string name="app_name">电话拨号器</string>
    <string name="phone">请输入手机号码</string>
    <string name="button">拨号</string>

</resources>

5、R.java资源类文件

/* AUTO-GENERATED FILE.  DO NOT MODIFY.
 *
 * This class was automatically generated by the
 * aapt tool from the resource data it found.  It
 * should not be modified by hand.
 */

package cn.huangjie.phone;

public final class R {
    public static final class attr {
    }
    public static final class drawable {
        public static final int ic_launcher=0x7f020000;
    }
    public static final class id {
        public static final int button=0x7f050001;
        public static final int mobile=0x7f050000;
    }
    public static final class layout {
        public static final int main=0x7f030000;
    }
    public static final class string {
        public static final int app_name=0x7f040001;
        public static final int button=0x7f040003;
        public static final int hello=0x7f040000;
        public static final int phone=0x7f040002;
    }
}
6、窗口和响应事件

package cn.huangjie.phone;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
	private EditText mobileText;
	
    /** Called when the activity is first created. 
     *  onCreate方法在整个Active的声明周期内只会执行一次
     * */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mobileText = (EditText) findViewById(R.id.mobile);
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new PhoneOnClickListener());
    }
    
    /**
     * 定义成内部类的话,在软件加载进虚拟机的话会比把该类定义在外部要快点,
     * 定义成内部类的话只要加载一个class文件即可,放在外部需要加载两个class文件
     */
    private final class PhoneOnClickListener implements View.OnClickListener{

		public void onClick(View v) {
			String phone = mobileText.getText().toString();
			Intent intent = new Intent();//意图
			intent.setAction("android.intent.action.CALL");
			intent.setData(Uri.parse("tel:" + phone));
//			intent.addCategory("android.intent.category.DEFAULT");
			startActivity(intent);//该方法内部会自动为intent添加类别意图,android.intent.category.DEFAULT
			
		}
    	
    }
    
}

示例资源下载路径: http://download.csdn.net/detail/wxwzy738/6240019

你可能感兴趣的:(一、android的电话拨号器)