您的位置:首页 > 移动开发

eatwhatApp开发实战(十四)

2016-03-05 12:34 225 查看

  之前我们就输入框EditText做了优化,而这次,我们为app添加拨打电话的功能。

  首先是布局,将activity_shop_info.xml中对应的电话那一栏进行重新设计:

  <RelativeLayout
android:id="@+id/ll_tel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/ll_name">
<TextView
android:id="@+id/tv_text_shop_tel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="电话:"
android:textSize="20sp"/>
<TextView
android:id="@+id/tv_shop_tel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/tv_text_shop_tel"
android:text="10086"
android:textSize="20sp"/>
<Button
android:id="@+id/btn_call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:onClick="call"
android:text="拨打"
android:layout_alignBaseline="@id/tv_shop_tel"
android:textSize="20sp"/>
</RelativeLayout>


  app中实现拨打电话的功能,在AndroidManifest.xml中必须添加权限:

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


  之后就可以写对应的实现代码:

    public void call(View v) {

// 获取电话号码栏中的号码
String num = tel_num.getText().toString();
// 如果输入不为空创建打电话的Intent
if (num.trim().length() != 0) {
Intent phoneIntent = new Intent("android.intent.action.CALL",
Uri.parse("tel:" + num));
// 启动
startActivity(phoneIntent);
}else {
// 否则Toast提示一下
Toast.makeText(ShopInfoActivity.this, "号码无效,或为空", Toast.LENGTH_LONG)
.show();
}
}


  这样,便完成了拨打电话的功能。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: