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


第一个Android小程序(Android电话拨号器),直接贴代码:

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

  1. <?xml version="1.0" encoding="utf-8"?>  
  • <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  •     android:orientation="vertical"  
  •     android:layout_width="fill_parent"  
  •     android:layout_height="fill_parent"  
  •     >  
  • <TextView    
  •     android:layout_width="fill_parent"   
  •     android:layout_height="wrap_content"   
  •     android:text="@string/inputmobile"  
  •     />  
  • <EditText   
  •     android:layout_width = "fill_parent"  
  •     android:layout_height"wrap_content"  
  •     android:id = "@+id/mobile" />  
  • <Button android:id="@+id/button"   
  •         android:layout_height="wrap_content"   
  •         android:text="@string/button"   
  •         android:layout_width="wrap_content">  
  • </Button>  
  • </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里面的。 

      相关阅读:

      Android开发之路——走进Android(工程结构剖析)
      Android开发之路——第一个Android小程序(Android电话拨号器)
      Android开发之路——第二个Android小程序(Android短信发送)
      Android开发之路——第三个Android小程序(Android的Activity显示)
      Android开发之路——Android的布局初步
      Android开发之路——Android的布局初步2——TableLayout布局
      Android开发之路——单选框,复选框,弹出框等控件操作
      Android开发学习笔记补充记录——Activity的生命周期

      接下来是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>

      下面是这个程序的截图:

       
    1. 相关内容