您的位置:首页 > 移动开发 > Android开发

使用Android开发打电话程序详细

2011-05-23 16:23 471 查看
       今天开发了我的第一个Android程序--打电话。虽然很简单,但是俗话说麻雀虽小五脏俱全。因为是小程序所以我按照的设计步骤是 1.设计界面。2.设计Activity。3.业务层代码。逐步实现。

<?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/hello"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/telphone"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn"
android:id="@+id/btnButton"
/>
</LinearLayout>
  这个就是layout中的设计界面在Android中界面由xml文件生成。其中需要注意的是:"android:id="@+id/btnButton"其中id就是按钮的id,定义的btnButton会自动在R.java中生成。

 

2. Activity中的代码如下:

public class HelloWorld extends Activity {
EditText text;
Button btn;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//根据id查找用户拨打的号码
text = (EditText)findViewById(R.id.telphone);
//根据id查找按钮
btn = (Button)findViewById(R.id.btnButton);
//将拨打按钮绑定到事件上
btn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
//定义Intent对象
Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+text.getText().toString()));
//启动Activity传输Intent
HelloWorld.this.startActivity(intent);
}
});
}
}


 

 这里需要特别提醒的是通过id查找按钮或文本框时是到R.java文件中查找相应的内部类中的常量,刚才我就是粗心写成了R.string.btn(到strings.xml中查找)害的我搞了很久都报一个错误这个错误也给大家看下。

 



 

这里就没有业务层代码的实现。

最后还要在AndroidManifest.xml中添加一句:打电话的权限 <uses-permission android:name="android.permission.CALL_PHONE" /> 勿忘! 在Android手机中会涉及到很多权限的问题要查阅帮助文档解决。

     上面的主要是针对小的程序,如果是大的项目的话就应该遵循1.业务层代码。2.设计界面。3.设计Activity的步骤。

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐