android练习一之拨号实现

//activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context="com.vincentlin.phonecall.MainActivity" 
    >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/inputmobile" 
        />
    <EditText 
        android:id="@+id/mobile"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
    <Button 
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button">
     </Button>
</LinearLayout>
//strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">电话拨号器</string>
    <string name="inputmobile">请输入手机号</string>
    <string name="button">请拨打此号码</string>
    <string name="action_settings">action_settings</string>

</resources>
//MainActivity.java
package com.vincentlin.phonecall;

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

public class MainActivity extends Activity {

    /**
     * 此方法是重载自启动方法,在Application启动的时候触发
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //调用到button按钮
        Button button = (Button) findViewById(R.id.button);
        //为button按钮设置监听器,监听器类型是在本视图的监听器
        button.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                //调用到编辑框的值
                EditText editText = (EditText) findViewById(R.id.mobile);
                //新建一个intent对象,进行调用系统的打电话的方式,然后传递号码过去
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + editText.getText()));
                //相应事件
                MainActivity.this.startActivity(intent);
            }
        });
    }

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

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="16" />
    <!-- 拨号权限 -->
    <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=".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>


你可能感兴趣的:(android,拨号)