Android实战教程第三篇之简单实现拨打电话功能

Android实战教程第三篇之简单实现拨打电话功能,第1张

概述本文实例为大家分享了Android打电话功能的实现代码,需要一个文本输入框输入号码,需要一个按钮打电话。

本文实例为大家分享了AndroID打电话功能的实现代码,需要一个文本输入框输入号码,需要一个按钮打电话。

本质:点击按钮,调用系统打电话功能。

xml布局文件代码::

<linearLayout 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"  tools:context=".MainActivity"  androID:orIEntation="vertical"  >   <TextVIEw   androID:layout_wIDth="wrap_content"   androID:layout_height="wrap_content"   androID:text="请输入号码" />  <EditText   androID:ID="@+ID/et_phone"   androID:layout_wIDth="match_parent"   androID:layout_height="wrap_content"   />  <button   androID:ID="@+ID/bt_call"   androID:layout_wIDth="wrap_content"   androID:layout_height="wrap_content"   androID:text="拨打"   />  </linearLayout> 

mainactivity中代码:

package com.ydl.dialer;  import androID.net.Uri; import androID.os.Bundle; import androID.app.Activity; import androID.content.Intent; import androID.vIEw.VIEw; import androID.vIEw.VIEw.OnClickListener; import androID.Widget.button; import androID.Widget.EditText;  public class MainActivity extends Activity {   @OverrIDe  protected voID onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentVIEw(R.layout.activity_main);      //给按钮设置点击侦听   //1.拿到按钮对象   button bt = (button) findVIEwByID(R.ID.bt_call);//button类是VIEw的子类,向下转型要强转。   //2.设置侦听   bt.setonClickListener(new MyListener());  }   class MyListener implements OnClickListener{    //按钮被点击时,此方法调用   @OverrIDe   public voID onClick(VIEw v) {    //获取用户输入的号码    EditText et = (EditText) findVIEwByID(R.ID.et_phone);    String phone = et.getText().toString();        //我们需要告诉系统,我们的动作:我要打电话    //创建意图对象    Intent intent = new Intent();    //把打电话的动作ACTION_CALL封装至意图对象当中    intent.setAction(Intent.ACTION_CALL);    //设置打给谁    intent.setData(Uri.parse("tel:" + phone));//这个tel:必须要加上,表示我要打电话。否则不会有打电话功能,由于在打电话清单文件里设置了这个“协议”        //把动作告诉系统,启动系统打电话功能。    startActivity(intent);   }     }   } 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android实战教程第三篇之简单实现拨打电话功能全部内容,希望文章能够帮你解决Android实战教程第三篇之简单实现拨打电话功能所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1147748.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存