Android模拟打电话应用程序实现分析


1、设计界面

 

今天学了一个小程序,实现简单的打电话功能。下面我来解析一下怎么来完成打电话的功能。

一、设计页面

二、Activity的开发

1.       我们先创建一个Android工程Phone

2.       在res文件下的values目录下的strings.xml中写入数据:

  <string name=”input_info”>请输入电话号码</string>

  <string name=”dial_caption”>拨打</string>

实现Android的数据传递。

3.       在layout中实现页面的布局,在main.xml中写:

<TextView

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="@string/input_info" />

    <!-定义文本框 -->

    <EditText

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:id="@+id/phone_number"/>

    <!-定义一个按钮 -->

    <Button

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="@string/dial_caption"

  android:id="@+id/dial_btn" />

4.       在PhoneActivity.java中写代码:

package cn.csdn.android;

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 {

    /** Called when the activity is first created. */

    EditText numberEt;

    Button dialBtn;

   

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

      

        findViews();

        dialBtn.setOnClickListener(new OnClickListener(){

 

           public void onClick(View v) {

              //调用系统的拨号服务实现电话拨打功能

              String phone_number=numberEt.getText().toString();

              phone_number=phone_number.trim();

              if(phone_number !=null && !phone_number.equals("")){

                  //封装一个拨打电话的intent,并且将电话号码包装成一个Uri对象传入

                  Intent intent=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+phone_number));

                  PhoneActivity.this.startActivity(intent);

                 

              }

             

           }

          

        });

    }

   

    public void findViews(){

       numberEt=(EditText) this.findViewById(R.id.phone_number);

       dialBtn=(Button) this.findViewById(R.id.dial_btn);

    }

}

  • 1
  • 2
  • 下一页

相关内容