读书人

Android开发从入门到精通(七) _8

发布时间: 2012-07-03 13:37:43 作者: rapoo

Android开发从入门到精通(7) _8

?

试试这个:修改AndoridPhoneDialer项目?第七章(8)

????如果你一直在搞最后版本的AndroidPhoneDialer,你或许发现有些东西错过了。不幸的是,项目当前的写作方式总是允许你输入任何类型的数值到EditText?View中,并且发送到Call活动中。这个真不是最佳的应用程序开发。研究一下并且增加验证到EditText中。使用下面的参数来修改项目:

●?使用常规的表达式来验证一个号码被输入到EditText中(package?java.regex)。?
●?使用showAlert(?)?语法来显示一条信息告诉用户他们输入的内容和你的常规表达式不匹配。当你觉得已经找到解决方案,和下面的代码做个比较:

main.xml

<?xml?version="1.0"?encoding="utf-8"?>?
<LinearLayout?xmlns:android=http://schemas.android.com/apk/res/android?
android:orientation="vertical"?
android:layout_width="fill_parent"?
android:layout_height="fill_parent"?
>?
<TextView?android:id="@+id/textLabel"?
android:layout_width="fill_parent"?
android:layout_height="wrap_content"?
android:text="Enter?Number?to?Dial:"?
/>?
<EditText?android:id="@+id/phoneNumber"?
android:layout_width="fill_parent"?
android:layout_height="wrap_content"?
/>?
<Button?android:id="@+id/callButton"?
android:layout_width="fill_parent"?
android:layout_height="wrap_content"?
android:layout_alignParentRight="true"?
android:text="Show?Dialer"?/>?
</LinearLayout>?
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()?{?
Chapter?7:?Using?Intents?and?the?Phone?Dialer?147?
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();?
}?
}?

当你运行本项目,它应当产生一个信息和下图类似(略)。

在下一章中,你将学习更多的Views。你将创建创建一个多活动应用程序来允许你浏览并创建本书中还没有谈论过的Views。你也会创建并利用一个菜单系统来启动你的活动。

问专家

Q:有办法来建立一个呼叫或者从模拟器中来确保这些活动正在工作吗?

A:当本书写的时候,还没有办法。但是,谷歌内部的讨论是在将来发布的SDK,开发者将有可能打开两个模拟器并且在两个模拟器之间呼叫。

Q:还有其它类型的按钮可用于活动吗?看上去或者感觉不一样的。

A:是的。你可以使用风格属性来创建小按钮,或者上下左右指示的小按钮。

题外话:到现在为止,第七章的翻译工作完成

?

更多信息请查看?http://www.javady.com/index.php/category/thread

读书人网 >Android

热点推荐