Android开发入门之拨打电话

第一步:新建一个Android工程命名为02.Phone目录结构如下图:

Android开发入门之拨打电话_第1张图片

第二步:修改activity_main.xml布局文件代码如下:

<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"
    tools:context=".MainActivity" >

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

    <EditText
        android:id="@+id/et_phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="phone" />

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

</LinearLayout>

strings.xml:

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

    <string name="app_name">02.Phone</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="please_enter_phone_number">请输入电话号码</string>
    <string name="call">呼叫此号码</string>

</resources>

第三步:编写MianActivity类:

package cn.leigo.phone;

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

public class MainActivity extends Activity {
	private EditText mPhoneEditText;

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

		mPhoneEditText = (EditText) findViewById(R.id.et_phone);
	}

	public void call(View v) {
		String number = mPhoneEditText.getText().toString();
		if (!TextUtils.isEmpty(number)) {
			Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
					+ number));
			startActivity(intent);
		}
	}

}


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

<uses-permission android:name="android.permission.CALL_PHONE" />

运行工程查看效果图:

Android开发入门之拨打电话_第2张图片

你可能感兴趣的:(android,拨打电话)