Android学习--电话拨号器

复习 转载自http://blog.csdn.net/hanmengaidudu/article/details/17203609

        实现一个简单的电话拨号器

        电话拨号器布局文件如下:

   

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <EditText   
  8.         android:id="@+id/et_number"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:inputType="phone"  
  12.         />  
  13.     <Button  
  14.         android:id="@+id/bt_dail"  
  15.         android:layout_width="match_parent"  
  16.         android:layout_height="wrap_content"  
  17.         android:layout_below="@id/et_number"  
  18.         android:text="@string/dail"/>"  
  19. </RelativeLayout>  
       只是一个简单的文本框,用于用户输入电话号码,和一个拨号按钮,用于拨打电话。

      详细参数介绍如下:

                      id:用于表示这个控件;

                      layout_wedth:表示这个控件的宽度。共有3个参数,match_parent和fill_parent是一样的意思,表示填满父窗体,wrap_content表示包裹内容。

     layout_height:表示控件的高度。

                     inputType:表示文本框输入的类型。phone为电话类型。

                     layout_below:表示这个控件位于哪个控件的下方。

                     text:表示这个控件上显示的类容。

   效果如下图:

        

      布局文件已经完成,那接下来就要对这些进行操作。具体如下:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.example.dail;  
  2.   
  3. import android.net.Uri;  
  4. import android.os.Bundle;  
  5. import android.renderscript.Int2;  
  6. import android.text.TextUtils;  
  7. import android.app.Activity;  
  8. import android.content.Intent;  
  9. import android.view.Menu;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.widget.Button;  
  13. import android.widget.EditText;  
  14. import android.widget.Toast;  
  15.   
  16. public class MainActivity extends Activity implements OnClickListener {  
  17.   
  18.     private Button bt_dail = null;  
  19.     private EditText et_number = null;  
  20.     @Override  
  21.     protected void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.activity_main);  
  24.         bt_dail = (Button) findViewById(R.id.bt_dail);  
  25.         et_number = (EditText) findViewById(R.id.et_number);  
  26.         bt_dail.setOnClickListener(new MyListener());  
  27.     }  
  28.     private void callPhone() {  
  29.         String number = et_number.getText().toString();  
  30.         if(TextUtils.isEmpty(number)) {  
  31.             Toast.makeText(MainActivity.this"号码不能为空"1).show();  
  32.         }  
  33.         Intent intent = new Intent();  
  34.         intent.setAction(Intent.ACTION_CALL);  
  35.         intent.setData(Uri.parse("tel:"+number));  
  36.         startActivity(intent);  
  37.     }  
  38.       
  39.     private class MyListener implements OnClickListener{  
  40.   
  41.         @Override  
  42.         public void onClick(View v) {  
  43.             // TODO Auto-generated method stub  
  44.             callPhone();  
  45.         }  
  46.           
  47.     }  
  48.   
  49. }  

首先,通过findViewById获取到对应的控件,然后就是对拨号按钮做监听,当按下拨号按钮的话,会调用MyListener这个监听器。

基本的已经完成,但是现在要拨打电话的话,还需要加上CALL_PHONE权限,要不然会出现进程无法运行的错误。如下所示:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.example.dail"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="8"  
  9.         android:targetSdkVersion="17" />  
  10. <!-- 电话服务权限 -->
  11.     <uses-permission android:name="android.permission.CALL_PHONE"/>  
  12.   
  13.     <application  
  14.         android:allowBackup="true"  
  15.         android:icon="@drawable/ic_launcher"  
  16.         android:label="@string/app_name"  
  17.         android:theme="@style/AppTheme" >  
  18.         <activity  
  19.             android:name="com.example.dail.MainActivity"  
  20.             android:label="@string/app_name" >  
  21.             <intent-filter> 
  22.                 <action android:name="android.intent.action.MAIN" />  
  23.   
  24.                 <category android:name="android.intent.category.LAUNCHER" />  
  25.             </intent-filter>  
  26.         </activity>  
  27.     </application>  
  28.   
  29. </manifest>  
接下来就可以布局到模拟器上运行了,运行效果如下图:

如果没有输入号码,就点击拨号的话就会如下图所示:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值