电话拨号器

1.在res/layout/activity_main.xml文件中,编辑界面控件代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context="com.example.call.MainActivity"
    tools:ignore="MergeRootFrame" 
    android:orientation="vertical" >

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

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

    <Button
        android:id="@+id/phoneButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/phoneButton" />

</LinearLayout>


2.在res/values/string.xml中,定义需要用到的字符串

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Call</string>
    <string name="action_settings">Settings</string>
    <string name="phoneLable">请输入号码</string>
    <string name="phoneButton">拨号</string>

</resources>

3.在MainActivity.java中,编写电话拨号器的逻辑代码

package com.example.call;

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 MainActivity extends Activity {

	private EditText phoneText = null;
	private Button phoneButton = null;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        phoneText = (EditText) this.findViewById(R.id.phoneText);
		phoneButton = (Button) this.findViewById(R.id.phoneButton);
		phoneButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String phoneStr = phoneText.getText().toString();
				Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:" + phoneStr));
				startActivity(intent);
			}
		});
        
    }
}

4.在AndroidManifest.xml中,声明打电话权限“android.permission.CALL_PHONE”

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.call"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.call.MainActivity"
            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>

运行效果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

走路诗人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值