电话拨号器

运行效果如下图所示:

布局文件activity_main.xml的代码:

<span style="font-size:18px;"><span style="font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_marginTop="20dp"
    android:layout_marginLeft="20dp"
    tools:context="com.gjj.phone.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/phoneText" />

    <EditText
        android:id="@+id/phoneNum"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="" >
    </EditText>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="call"
        android:text="@string/button" />

</LinearLayout></span></span>

strings.xml文件中的代码:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">电话拨号器</string>
    <string name="phoneText">请输入手机号</string>
    <string name="button">拨号</string>
    <string name="action_settings">Settings</string>
</resources>


MainActivity.java中的代码:

<span style="font-size:18px;">package com.gjj.phone;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	public void call(View v) {
		EditText phoneText = (EditText) findViewById(R.id.phoneNum);
		String num = phoneText.getText().toString();
		// 创建意图对象,通过意图过滤器激活系统自带的电话拨号器的Activity
		Intent intent = new Intent();
		intent.setAction("android.intent.action.CALL");
		intent.setData(Uri.parse("tel:" + num));
		startActivity(intent);
	}
}</span>


最后在AndroidManifest.xml中要添加权限:

<span style="font-size:18px;"><uses-permission android:name="android.permission.CALL_PHONE"/></span>


下载源码





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值