---
title: "第七章\t使用Intents与Phone Dialer——下"
author: "wc0903"
date: "2010-03-02T09:23:04Z"
tags: []
categories: ["移动开发"]
slug: "intents-phone-dialer"
draft: false
lastmod: "2010-03-02T09:23:55Z"
id: 605367
url: "https://wc0903.iteye.com/blog/605367"
---

**实现EditText视图**

你需要为你的Activity添加爱一个视图，可以使用户输入一些文本。然后你解析这些文本并将它发送给前一节的Intent call。因为所有视图都是从基本视图集成而来。在架构和用法方面他们会非常相似。你会发现实现EditText非常简单。

首先，在main.xml中布局视图。实际上，你会在这里添加两个视图：一个TextView作为标签并给用户提示，和一个EditText来接受用户的输入。这两个视图会为你的Activity增加所需的深度和实用性。

如同你看到的Activity，记住.xml文件也是可视化的。这就意味着如果你想在最终的Activity上让TextView出现在EditText之上，你应在main.xml中将它放在EditText之前。

因为你已经使用了好几次TextView，这里就不在赘述视图的创建了。仅仅看一下TextView中你设置的属性。

&lt;TextView android:id="@+id/textLabel"

android:layout\_width="fill\_parent"

android:layout\_height="wrap\_content"

android:text="Enter Number to Dial:"/&gt;

这再平常不过了。这仅仅是一个简单的TextView，文本为Enter Number to Dial：。这个TextView会作为EditView的一个标签使用。下面是EditViw属性设置的内容：

&lt;EditText android:id="@+id/phoneNumber"

android:layout\_width="fill\_parent"

android:layout\_height="wrap\_content"

/&gt;

Id设置为phoneNumber，它是哦你在代码中用来和EditionText关联的名字。再次，设main.xml也很稀松平常。你最终的代码看起来像：

&lt;?xml version="1.0" encoding="utf-8"?&gt;

&lt;LinearLayout xmlns:android=http://schemas.android.com/apk/res/android

android:orientation="vertical"

android:layout\_width="fill\_parent"

android:layout\_height="fill\_parent"

&gt; 

&lt;TextView android:id="@+id/textLabel"

android:layout\_width="fill\_parent"

android:layout\_height="wrap\_content"

android:text="Enter Number to Dial:"

/&gt;

&lt;EditText android:id="@+id/phoneNumber"

android:layout\_width="fill\_parent"

android:layout\_height="wrap\_content"

/&gt;

&lt;Button android:id="@+id/callButton"

android:layout\_width="fill\_parent"

android:layout\_height="wrap\_content"

android:layout\_alignParentRight="true"

android:text="Show Dialer" /&gt;

&lt;/LinearLayout&gt;

现在完成了main.xml。你可以转移到AndroidPhoneDialer.java上了——本章中的上项目——你可能会参考上一届看看.java文件中添加了什么代码。这能保证你从代码中的正确的点开始。

你需要添加到.java文件中的第一项是包定义。你不仅需要为Uri，Button和Intent添加包，也需要为EditText添加：

import android.widget.Button;

import android.content.Intent;

import android.net.Uri;

import android.widget.EditText;

创建EditView视图的语法和创建Button的相同：

final EditText &lt;edittext\_name&gt; = &lt;edittext&gt;

再次，调用你的EditText phoneNuumber。创建EditText的代码如下：

final EditText phoneNumber = (EditText) findViewById(R.id.phoneNumber);

一旦你的phoneNumber EditText被创建了，你就可以使用它关联设备上输入的文本。现在你需要做的是调用phoneNumber.getText()来获取用户的输入。替换掉下一个行中的硬编码的值“tel:5551212”,

Intent(Intent.CALL\_ACTION,Uri.parse("tel:5551212"));

替换为getText()的值：

Intent(Intent.CALL\_ACTION,Uri.parse("tel:" + phoneNumber.getText()));

这就是你需要更新的全部代码。增加这两小处后，你可以给你的用户一个输入电话号码的对象，并将号码发给电话的Call Activity。..java文件的全部代码如下：

package android\_programmers\_guide.AndroidPhoneDialer;

import android.app.Activity;

import android.os.Bundle;

import android.widget.Button;

import android.view.View;

import android.content.Intent;

import android.net.Uri;

import android.widget.EditText;

public class AndroidPhoneDialer extends Activity {

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

@Override

public void onCreate(Bundle icicle) {

super.onCreate(icicle);

setContentView(R.layout.main );

final EditText phoneNumber = (EditText) findViewById(R.id.phoneNumber

);

final Button callButton = (Button) findViewById(R.id.callButton);

callButton.setOnClickListener(new Button.OnClickListener() {

public void onClick(View v){

Intent CallIntent = new

Intent(Intent.CALL\_ACTION,Uri.parse("tel:" + phoneNumber.getText()));

CallIntent.setLaunchFlags(Intent.NEW\_TASK\_LAUNCH );

startActivity(CallIntent);

}

});

}

}

当你在模拟器中运行应用时，你会看到和如下插图类似的屏幕：

![](http://dl.iteye.com/upload/attachment/210992/af9ff0ee-1279-3ea6-89f2-fd4a49650e69.jpg)

**尝试更改AndroidPhoneDialer**

如果你玩透了AndroidPhoneDialer的最新版本，你会注意到缺少什么东西。不幸的是，现在项目的编写方式，可以允许你输入任何类型的值，并把它们发送给CallActivity。这实在不是一个最佳的应用开发方案。

做些研究并为EditText增加一些验证。使用下面的参数更改你的项目：

● 使用正则表达式验证EditText中输入的电话号码，在java.regex包中。

● 使用showAlert()语法，显示信息告诉用户输入了不合正则表达式的内容。

当你觉得干的好不错时，和下面的代码做个比较。

**main.xml**

&lt;?xml version="1.0" encoding="utf-8"?&gt;

&lt;LinearLayout xmlns:android=http://schemas.android.com/apk/res/android

android:orientation="vertical"

android:layout\_width="fill\_parent"

android:layout\_height="fill\_parent"

&gt; 

&lt;TextView android:id="@+id/textLabel"

android:layout\_width="fill\_parent"

android:layout\_height="wrap\_content"

android:text="Enter Number to Dial:"

/&gt;

&lt;EditText android:id="@+id/phoneNumber"

android:layout\_width="fill\_parent"

android:layout\_height="wrap\_content"

/&gt;

&lt;Button android:id="@+id/callButton"

android:layout\_width="fill\_parent"

android:layout\_height="wrap\_content"

android:layout\_alignParentRight="true"

android:text="Show Dialer" /&gt;

&lt;/LinearLayout&gt;

 

**AndroidPhoneDialer.java**

package android\_programmers\_guide.AndroidPhoneDialer;

import android.app.Activity;

import android.os.Bundle;

import android.widget.Button;

import android.view.View;

import android.content.Intent;

import android.net.Uri;

import android.widget.EditText;

import java.util.regex.\*;

public class AndroidPhoneDialer extends Activity {

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

@Override

public void onCreate(Bundle icicle) {

super.onCreate(icicle);

setContentView(R.layout.main );

final EditText phoneNumber = (EditText)

findViewById(R.id.phoneNumber );

final Button callButton = (Button) findViewById(R.id.callButton);

callButton.setOnClickListener(new Button.OnClickListener() {

 

public void onClick(View v){

if (validatePhoneNumber(phoneNumber.getText().toString())){

Intent CallIntent = new

Intent(Intent.CALL\_ACTION,Uri.parse("tel:" + phoneNumber.getText()));

CallIntent.setLaunchFlags(Intent.NEW\_TASK\_LAUNCH );

startActivity(CallIntent);

}

else{

showAlert("Please enter a phone number in the X-XXX-XXX-XXXX

format.",0, "Format Error", "Re-enter Number",false);

}

}

});

}

public boolean validatePhoneNumber(String number){

Pattern phoneNumber = Pattern.compile("(\\\\d-)?(\\\\d{3}-)?\\\\d{3}

\\\\d{4}");

Matcher matcher = phoneNumber.matcher(number);

return matcher.matches();

}

}

当你运行该项目，你会输出一条如下的类似信息。

![](http://dl.iteye.com/upload/attachment/210994/aa774d60-69f0-3cb7-a662-e78d26209d3e.jpg)

下一章，你会学习更多的试图，你会创建一些多Activity的应用，使你能够探索并创建本书中还没提过的视图。你也会创建并利用一个菜单系统来启动你的Activity。

 

**专家答疑**

**问：有没有一种从模拟器中接打电话的方式，来验证这些Activity是否可用。？**

**答：**截止到本书编写之时，还么有一种方式能够从模拟器完成电话的接打。然而Google的未来发布的SDK中有相关的话题，开发者可以启动两个模拟器，在两者之间来完成通话。

**问：有没有其它类型的供Activity可用的按钮——拥有不同的外观或感觉？**

**答：**有。你可以使用样式属性，来创建小按钮或拥有向上，向下，向左，向右指针的小按钮。