第76页
(3)启动应用程序,如图6.7所示。单击“转到下一个Activity”按钮,如图6.8所示。此时并没有让用户选择处理隐式Intent的组件,而是直接跳转到第二个Activity。
图6.7 第一个Activity界面 图6.8 第二个Activity界面
6.3 经典范例
6.3.1 使用Intent拨打电话
例6.5 在Eclipse中创建Android项目,名称为6.5,实现拨打电话功能。(实例位置:光盘\TM\sl\6\6.5)
(1)在res\layout文件夹中打开布局文件main.xml。添加一个编辑框和一个按钮,并修改其默认属性,其代码如下:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
android:orientation="vertical" >
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:textColor="@android:color/black"
android:textSize="25px" >
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/call"
android:textColor="@android:color/black"
android:textSize="25px" />
(2)编写DialActivity,它从页面中获得用户输入的电话号码。通过为按钮增加单击事件监听器来完成拨号功能,其代码如下:
public class DialActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); //设置页面布局
EditText numberTV = (EditText) findViewById(R.id.editText); //通过id值获得编辑框对象
final String number = numberTV.getText().toString(); //获得用户输入的电话号码
Button dial = (Button) findViewById(R.id.button); //通过id值获得按钮对象
dial.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(); /创建Intent对象
intent.setAction(Intent.ACTION_CALL); //为Intent设置动作
intent.setData(Uri.parse("tel:" + number)); //为Intent设置数据
startActivity(intent); //将Intent传递给Activity
}
});
}
}
(3)修改AndroidManifest.xml文件,增加拨打电话的权限,其代码如下:
package="com.mingrisoft"
android:versionCode="1"
android:versionName="1.0" >
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
(4)运行应用程序,效果如图6.9所示。在编辑框中输入需要拨打的电话,单击“拨打电话”按钮就可以完成拨号功能。
图6.9 拨打电话界面
6.3.2 使用Intent打开网页
例6.6 在Eclipse中创建Android项目,名称为6.6,实现打开网页功能。(实例位置:光盘\TM\sl\6\6.6)
(1)在res\layout文件夹中打开布局文件main.xml。添加一个按钮,并修改其默认属性,其代码如下:
android:layout_width="fill_parent"
android:layout_height="fill_parent"