Android模拟打电话程序实现

首先建一个新项目叫Myphone

1、界面设计很简单

打开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/text"
	    />
	<Button 
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:id="@+id/button"
	   	android:text="@string/button"
	    />
</LinearLayout>

上文件的TextView是一个类似java中的label标签,相当于html中的input控件,用以输入的视图控件!EditText类似于java中的文本框,可编辑的文本框,输入内容!Button就是一个按钮!

代码 :
<?xml version="1.0" encoding="utf-8"?>
XML (Extensible Markup Language) 是一种标记描述语言,不管是语法还是看起来的样子,都相当类似网页所使用的 HTML 标记语言。 XML 被广泛地运用在 Java 程序的设定 中 。 main.xml文件里,第一行是每个 XML 描述档固定的开头内容,用来指示这个文字档 桉是以 XML 格式描述的。

代码 :
<LinearLayout></LinearLayout>" 线性版面配置 "(LinearLayout) 标签,使用了两个「 LinearLayout 」标签,来表示一个界面元件的区块。后头的标签前加上一个「 / 」 符号来表示结束标签。 " 线性版面配置 " 所指的是包含在 「 LinearLayout 」标签中,所有元件的配置方式,是将一个接一个元件由上而下排队排下来的意思。

代码 :
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns 开头的这串叙述,是用来宣告这个 XML 描述档桉的的名称空间 (NameSpace) , 后面接的 URL( 网址 ) ,表示这个描述档桉会参照到 Android 名称空间提供的定义。 所有Android 版面配置档桉的最外层标签中,都必须包含这个属性。

android:layout_width定义宽度,它有fill_parent(填满整个上层元件),wrap_content值,还有自定义一下值以px(是屏幕的像素点)、in(英寸)、mm(毫米)、pt(磅1/72英寸)、dp(一个基于density的抽象单位,如果一个160dpi的屏幕,1dp=1px)、dip(等同于dp)、sp(同dp相似,但是还会根据用户的字体大小偏好来缩放);建议使用sp作为文本的单位,其它用dip!!

android:layout_height定义高度,同上!!

androd:orientation="vertical"版本走向,垂直走!!

android:id="@+id/text"是定义该组件的id

android:text="@string/button"文本是string.xml文件中定义的;

:2、编写strings.xml文件

<?xml version="1.0" encoding="utf-8"?>
<resources>    
    <string name="app_name">PhoneActivity</string>
    <string name="phone">请输入电话号码:</string>
	<string name="button">拔打电话</string>
</resources>
app_name这行代码,用来表示应用程序名称

在 strings.xml 档桉中,我们在原本的 app_name 字串外,自行定义了另外几个字串 。
如果我们再次开启 「 R.java 」档,我们会发现档桉中的 string 类别中也自动索引了上面
定义好的字串:
R.java代码 :

 public static final class string {
        public static final int app_name=0x7f040001;
        public static final int button=0x7f040002;
        public static final int phone=0x7f040000;
    }
我们把这些字串应用到之前定义好的 XML 描述档中。透过使用 @string/[ 识别 符号 ] 这样存取 string 类型的格式,来取代 main.xml 档桉中原本写死的文字叙述。
3、编写PhoneActivity类

package cn.csdn.android.heyley;

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

public class PhoneActivity extends Activity {
    EditText editText;
    Button button;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findView();
        button.setOnClickListener(new OnClickListener() {			
			public void onClick(View v) {
				String phoneNum=editText.getText().toString().trim();
				if(phoneNum!=null&&!phoneNum.equals("")){
					Intent intent=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+phoneNum));
					PhoneActivityActivity.this.startActivity(intent);
				}
				
			}
		});
    }
    public void findView(){
    	editText=(EditText) this.findViewById(R.id.text);
    	button=(Button) this.findViewById(R.id.button);
    }
}
该类直接就继承了Activity,自动生成方法onCreate(Bundle saveInstanceState);当 Android 应用程序启动、换到背景等待、关闭时,都会用到 "savedInstanceState" 这
个实体来处理记忆体相关的事宜。当然,你也可以用其他名称来代替它。还好 "onCreate" 这个方法永远都 是传入 "Bundle savedInstanceState" 这个参数,写应用程序时只要正确照规定传入即可。

setContentView(R.layout.main);
透过萤幕显示的各种元素是按照界面层次结构来描述的。要将一个显示元素的层次结 构转换显示到一个萤幕上, Activity 会呼叫它用来设定 View 的 "setContentView" 方法,并
传入想引用的 XML 描述文件。当 Activity 被启动并需要显示到萤幕上时,系统会通知Activity ,并根据引用的 XML 文件叙述来描绘出使用者界面。

 "setContentView" 方法确实是透过 "R.layout.main" 来引用 XML 文件描述文档的资源,而不是直接透过 res 目录来引用。
Button button = (Button)findViewById(R.id.submit);宣告一个 button 实体,透过 findViewById 方法,从资源文档中取得对应的界面元件 ( 按钮);editText也是同理;

button.setOnClickListener(new OnClickListener(){});这句包含了 " button.setOnClickListener" 与其中的 "OnClickListener" 两个类别。"setOnClickListener" 是 button (按钮)实体的方法。

String phoneNum = editText.getText().toString();
得到该文本框中输入内容的 String 值。

Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+phoneNum));
我们所做的电话拨号并不需要自己实现拨打电话的功能,而是调用系统的拨号服务 , 由拨号服务来帮我们拨打电话 。 在介绍如何调用系统拨号服务之前 , 我们先来了解下在 W indows系统下的一个与其类似的操作。

4、申请拔号权限

在AndroidManifest.xml中定入:

< uses-permission android:name = "android.permission.CALL_PHONE" />

5、运行程序效果如图

ok,这样一个简单的拔号应用程序写完了!!!!

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值