经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android studio案例之实现电话拨号
来源:jb51  时间:2021/4/12 13:52:23  对本文有异议

 一、代码配置

1、创建项目

流程看图

2、增添代码

更改布局

布局完整代码

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. tools:context=".MainActivity">
  9.  
  10. <TextView
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="电话拨号"
  14. app:layout_constraintBottom_toBottomOf="parent"
  15. app:layout_constraintLeft_toLeftOf="parent"
  16. app:layout_constraintRight_toRightOf="parent"
  17. app:layout_constraintTop_toTopOf="parent" />
  18.  
  19. </LinearLayout>

插入图片

activity_main_xml文件布局

完整代码

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. tools:context=".MainActivity">
  9.  
  10. <TextView
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="电话拨号"
  14. app:layout_constraintBottom_toBottomOf="parent"
  15. app:layout_constraintLeft_toLeftOf="parent"
  16. app:layout_constraintRight_toRightOf="parent"
  17. app:layout_constraintTop_toTopOf="parent" />
  18. <EditText
  19. android:layout_width="match_parent"
  20. android:layout_height="wrap_content"
  21. android:hint="输入电话号码"
  22. android:inputType="number"
  23. android:id="@+id/phoneNum"/>
  24. <RelativeLayout
  25. android:layout_width="match_parent"
  26. android:layout_height="match_parent">
  27. <ImageButton
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:background="@drawable/call"
  31. android:layout_centerInParent="true"
  32. android:id="@+id/call_btn"/>
  33. </RelativeLayout>
  34. </LinearLayout>

效果展示

mainactivity.java文件

  1. EditText phoneNum;
  2. ImageButton call_btn;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7. phoneNum=(EditText) findViewById(R.id.phoneNum);
  8. call_btn=(ImageButton) findViewById(R.id.call_btn);
  9. call_btn.setOnClickListener(new View.OnClickListener() {
  10. @Override
  11. public void onClick(View v) {
  12. Intent intent=new Intent();
  13. intent.setAction(Intent.ACTION_CALL);
  14. intent.setData(Uri.parse("tel:"+phoneNum.getText()));
  15. startActivity(intent);

图片

运行代码

拨号尝试,出现报错,需要设置对应权限

3、权限请求

Androidmainfest.xml文件中

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

Android 6.0以上需要自己手动赋予权限。

应用程序权限请求
版本号判断方法

  1. protected boolean shouldAskPermissions(){
  2. return (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1);
  3. }

权限申请方法

  1. protected void askPermissions() {
  2. String[] permissions = {
  3. "android.permission.CALL_PHONE"
  4. };
  5. int requestCode = 200;
  6. requestPermissions(permissions, requestCode);
  7. }

在onCreate中调用

  1. if(shouldAskPermissions()){
  2. askPermissions();
  3. }

请求权限加入代码时要保证程序处于运行状态,不然代码加进去会报错。

二、效果演示

随便输入得数字号码,提示为空号。

以上就是Android studio案例之实现电话拨号的详细内容,更多关于Android studio电话拨号的资料请关注w3xue其它相关文章!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号