Android实现打电话功能


初学安卓,入门的应用 。打电话。


新建Android 项目



layout 控件布局 :activity_main.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/phoneview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/lable_phone" />

    <EditText
        android:id="@+id/editview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/phoneview"
        android:layout_below="@+id/phoneview"
        android:layout_marginTop="14dp"
        android:ems="10"
        android:inputType="phone" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/butphone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editview"
        android:layout_below="@+id/editview"
        android:layout_marginTop="16dp"
        android:text="@string/butphone" />

</RelativeLayout>


在AndroidManifest.xml 文件 中为其添加 打电话的 权限



  

你会在你发现在AndroidManifest.xml 文件 中有了一断这样的代码 其为打电话的 权限

//设置拨打电话 的权限
    <uses-permission android:name="android.permission.CALL_PHONE"/>
    <uses-permission />


上面已经布局好 了。下面

然后在 MainActivity  中实现其功能:


public class MainActivity extends Activity {
private EditText editText;
private Button button_phone;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置显示pylt
setContentView(R.layout.activity_main);

//获取按钮组件
button_phone=(Button) findViewById(R.id.butphone);
//获取输入框组件
editText = (EditText) findViewById(R.id.editview);

//注册按钮事件
button_phone.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//获取号码
String phone_Num = editText.getText().toString();

//定义执行打电话的意图对象
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+phone_Num));

//执行意图
MainActivity.this.startActivity(intent);

//吐司的效果
Toast.makeText(MainActivity.this, "正在给"+phone_Num+"打电话",Toast.LENGTH_LONG).show();

}
});


}

}


然后把你写好的项目 发布到你的虚拟机上,Ok .








  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值