使用Android开发打电话程序

来源:互联网 发布:淘宝上那家衣服质量好 编辑:程序博客网 时间:2024/05/16 10:14

 今天开发了我的第一个Android程序--打电话。

        因为是小程序所以我按照的设计步骤是 1.设计界面。2.设计Activity。3.业务层代码。逐步实现。

[xml] view plaincopy
  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/hello"  
  11.     />  
  12. <EditText    
  13.     android:layout_width="fill_parent"   
  14.     android:layout_height="wrap_content"   
  15.     android:id="@+id/telphone"  
  16.     />  
  17.  <Button   
  18.     android:layout_width="wrap_content"   
  19.     android:layout_height="wrap_content"   
  20.     android:text="@string/btn"  
  21.     android:id="@+id/btnButton"  
  22.     />  
  23. </LinearLayout>  
  这个就是layout中的设计界面在Android中界面由xml文件生成。
其中需要注意的是:"android:id="@+id/btnButton"其中id就是按钮的id,定义的btnButton会自动在R.java中生成。
android:text="@string/hello"  中的hello是在项目res/values下的strings.xml中定义的string,内容自己添加。
2. Activity中的代码如下:
package com.example.phonecall;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;import android.app.Activity;import android.view.Menu;public class HelloWorld extends Activity {EditText text;Button btn;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);      //根据id查找用户拨打的号码        text = (EditText)findViewById(R.id.telphone);      //根据id查找按钮        btn = (Button)findViewById(R.id.btnButton);      //将拨打按钮绑定到事件上        btn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//定义Intent对象Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+text.getText().toString()));//启动Activity传输IntentHelloWorld.this.startActivity(intent);}});    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    }
这里需要特别提醒的是通过id查找按钮或文本框时是到R.java文件中查找相应的内部类中的常量

3. strings.xml
<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">PhoneCall</string>    <string name="action_settings">Settings</string>    <string name="hello_world">Hello world!</string>    <string name="hello">拨出号码:</string>    <string name="btn">拨打</string></resources>

4. 添加打电话权限AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.phonecall"
    android:versionCode="1"
    android:versionName="1.0" >


    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
    <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.phonecall.HelloWorld"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>


</manifest>
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 买房的合同丢了怎么办 押金的收据丢了怎么办 房东的合同掉了怎么办 个人档案里单位没有放合同怎么办 签的合同掉了怎么办 一方合同弄丢了怎么办 合同丢了怎么办如何补 签了定金合同对方违约怎么办 医学出生证明丢了怎么办 易通行出站未刷怎么办 炭烧酸奶过期了怎么办 西安建行etc坏了怎么办 电机在设备壳体中拔不出来怎么办 公司变更股东不能亲临现场怎么办? 公司股东变更老股东不签字怎么办 公司变更地址股东不签字怎么办 公司变更股份股东不签字怎么办 公司股东离职股东没变更过来怎么办 河南省宋基投资公司欠钱怎么办 曲江楼观2O18怎么办 华旭金卡身份证扫描不了怎么办 水表里有钱没水怎么办? ff14过图速度慢怎么办 想让电表跑的慢怎么办 家里电表突然没有电了怎么办 电表不识别电卡怎么办 家里水表不转了怎么办 车管所体检色弱怎么办 煤气押金单没了怎么办 中国建设银行登录密码忘了怎么办 中国建设银行登录密码忘记了怎么办 公司车辆怎么办换新能源牌 杭州新能源汽车牌照外地人怎么办 建行登录密码忘了怎么办 新捷达epc灯亮怎么办 捷达车玻璃升降偏离怎么办 交金中断一个月怎么办 博士拟录取没导师怎么办 保研联系导师后怎么办 特别害怕和导师交流怎么办 面试工资要少了怎么办