android 电话拨号器

简单界面截图如下:

android 电话拨号器_第1张图片


一、因为要使用系统的电话拨号服务,所以首先要在AndroidManifest.xml清单文件中添加该权限:

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="8" />

    <!-- 添加电话拨号权限 -->
    <uses-permission android:name="android.permission.CALL_PHONE" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".DialerActivity"
            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>


二、在strings.xml中增加需要的变量

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

    <string name="hello">Hello World, DialerActivity!</string>
    <string name="app_name">Dialer</string>
    <string name="inputPhoneNumber">请输入电话号码:</string>
    <string name="dialer">呼叫</string>
    <string name="empty_phonenumber">电话号码不能为空</string>

</resources>


三、编写界面layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

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

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

        <requestFocus />
    </EditText>

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

</LinearLayout>

四、编写activity

package com.example.Dialer;

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

public class DialerActivity extends Activity {
	private EditText mPhoneNumber = null;
	private Button mDialButton = null;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mPhoneNumber = (EditText)findViewById( R.id.etPhoneNumber );
        mDialButton = (Button)findViewById( R.id.buttonDialer );
        mDialButton.setOnClickListener( new OnClickListener( ) {
			
			@Override
			public void onClick( View v ) {
				String phoneNumber = mPhoneNumber.getText( ).toString( );
				if ( phoneNumber.equals( "" ) ) {
					Toast.makeText( getApplicationContext( ), R.string.empty_phonenumber, Toast.LENGTH_LONG ).show( );
					return;
				}
				
				Intent intent = new Intent( Intent.ACTION_CALL, Uri.parse( "tel:" + phoneNumber ) );
				startActivity( intent );
			}
		} );
    }
}

你可能感兴趣的:(android,String,layout,button,encoding,电话)