Android之路——第一个Android小程序(Android电话拨号器)

废话不说,直接贴代码:

首先是布局的代码:main.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="@string/inputmobile"  
  11.     />  
  12. <EditText   
  13.     android:layout_width = "fill_parent"  
  14.     android:layout_height"wrap_content"  
  15.     android:id = "@+id/mobile" />  
  16. <Button android:id="@+id/button"   
  17.         android:layout_height="wrap_content"   
  18.         android:text="@string/button"   
  19.         android:layout_width="wrap_content">  
  20. </Button>  
  21. </LinearLayout>  
 

接下来是String.xml的代码:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="inputmobile">请输入手机号:</string>  
  4.     <string name="app_name">电话拨号器</string>  
  5.     <string name = "button">拨打此号码</string>  
  6. </resources>  
 

上面两个都是位于res里面的。

 

接下来是src里面的代码DialogActivity:

  1. package cn.jason.android;  
  2. import android.app.Activity;  
  3. import android.content.Intent;  
  4. import android.net.Uri;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.widget.Button;  
  8. import android.widget.EditText;  
  9. public class DialogActivity extends Activity {  
  10.     /** Called when the activity is first created. */  
  11.     @Override  
  12.     /** 
  13.      * 这个方法是重载自启动方法,在Application启动的时候自动触发 
  14.      */  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18.         //调用到button按钮  
  19.         Button button = (Button) findViewById(R.id.button);  
  20.         //为button按钮设置监听器,监听器类型是在本视图的监听器  
  21.         button.setOnClickListener(new View.OnClickListener() {  
  22.               
  23.             public void onClick(View view) {  
  24.                 //调用到编辑框的值  
  25.                 EditText editText = (EditText) findViewById(R.id.mobile);  
  26.                 //新建一个intent对象,进行调用系统的打电话的方法,然后传递号码过去  
  27.                 Intent intent = new Intent(Intent.ACTION_CALL , Uri.parse("tel:" +  editText.getText()));  
  28.                 //相应事件  
  29.                 DialogActivity.this.startActivity(intent);  
  30.             }  
  31.         });  
  32.     }  
  33. }  
 

 

这个代码写完还是不能用的,因为没有授权,所以必须在androidManifest这个文件里面写:<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>

下面是这个程序的截图:

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值