Android开发教程:拨打电话的简单实现

Linux大全评论599 views阅读模式

Android打电话主要就是用内部类加上监听器实现的,比较的简单和粗糙,这里没有进行什么美化。用到的是以前所用的MVC模式,其中main.xml就相当于view视图层,主要是通过activity文件的调用从而达到视图界面的搭建

首先是页面的实现,页面设计的只是功能用到的一些东西。在string.xml中定义文字提示信息,下面是string.xml的部分代码:

  1.  <string name="app_name">拨打电话</string>  
  2.  <string name="myPhone">请输入您要拨打的号码:</string>  
  3. <string name="dial_phone">拨打</string>  
  1. //定义显示标签的提示信息  
  2.   
  3. <TextView   
  4.   
  5.         android:layout_width="fill_parent"  
  6.   
  7.         android:layout_height="wrap_content"  
  8.   
  9.         android:text="@string/myPhone"/>  
  10.   
  11. //输入文本框的显示风格  
  12.   
  13.    <EditText  
  14.   
  15.         android:layout_width="fill_parent"  
  16.   
  17.         android:layout_height="wrap_content"  
  18.   
  19.         android:id="@+id/phone_number"/>  
  20.   
  21.     //按钮的显示及其按钮的值  
  22.   
  23.     <Button   
  24.   
  25.         android:layout_width="wrap_content"  
  26.   
  27.         android:layout_height="wrap_content"  
  28.   
  29.         android:layout_gravity="center"  
  30.   
  31.         android:id="@+id/dial_btn"  
  32.   
  33.         android:text="@string/dial_phone" />  
  1. EditText numberEt;  
  2.   
  3.     Button dialBtn;  
  4.   
  5.      
  6.   
  7.     public void onCreate(Bundle savedInstanceState) {  
  8.   
  9.         super.onCreate(savedInstanceState);  
  10.   
  11.         setContentView(R.layout.main);  
  12.   
  13.           
  14.   
  15.         findViews();  
  16.   
  17.         /*创建内部监听类*/  
  18.   
  19.         dialBtn.setOnClickListener(new OnClickListener() {  
  20.   
  21.          
  22.   
  23.        @Override  
  24.   
  25.        public void onClick(View v) {  
  26.   
  27.          // TODO Auto-generated method stub  
  28.   
  29.          //调用系统的拨号服务实现电话拨打功能  
  30.   
  31.          String phone_number = numberEt.getText().toString();  
  32.   
  33.          //对字符串进行去空格操作  
  34.   
  35.          phone_numberphone_number = phone_number.trim();  
  36.   
  37.          if(phone_number!=null && !phone_number.equals("")){  
  38.   
  39.             //封装一个拨打电话的intent,并且将电话号码封装成一个Uri对象传入  
  40.   
  41.             Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+ phone_number));  
  42.   
  43.             PhoneActivity.this.startActivity(intent);  
  44.   
  45.          }  
  46.   
  47.    
  48.   
  49.        }  
  50.   
  51.      });  
  52.   
  53.     }  
  54.   
  55.     public void findViews(){  
  56.   
  57.     numberEt = (EditText) this.findViewById(R.id.phone_number);  
  58.   
  59.     dialBtn = (Button) this.findViewById(R.id.dial_btn);  
  60.   
  61.     }  

企鹅博客
  • 本文由 发表于 2020年7月24日 14:29:24
  • 转载请务必保留本文链接:https://www.qieseo.com/172963.html

发表评论