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

本文介绍了如何使用Android开发手机拨号器应用,包括布局设计、字符串资源定义、核心功能实现及权限配置,并提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

废话不说,直接贴代码:

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

<?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的代码:

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

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

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

package cn.jason.android; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class DialogActivity extends Activity { /** Called when the activity is first created. */ @Override /** * 这个方法是重载自启动方法,在Application启动的时候自动触发 */ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //调用到button按钮 Button button = (Button) findViewById(R.id.button); //为button按钮设置监听器,监听器类型是在本视图的监听器 button.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { //调用到编辑框的值 EditText editText = (EditText) findViewById(R.id.mobile); //新建一个intent对象,进行调用系统的打电话的方法,然后传递号码过去 Intent intent = new Intent(Intent.ACTION_CALL , Uri.parse("tel:" + editText.getText())); //相应事件 DialogActivity.this.startActivity(intent); } }); } }

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

下面是这个程序的截图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值