首页 > 代码库 > (Android 基础知识review)打电话

(Android 基础知识review)打电话

1、main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/a"
        />
    
    <EditText 
        android:id="@+id/num_et"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/c"
        />
    
    <Button 
        android:id="@+id/call_bt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/b"
        />
</LinearLayout>


2、MainActivity

package com.njupt.phonetest;

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

public class MainActivity extends Activity {

	private EditText et_num;
	private Button bt_call;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		
		et_num = (EditText) findViewById(R.id.num_et);
		bt_call = (Button) findViewById(R.id.call_bt);
		
		bt_call.setOnClickListener(new MyOnClickListener());
	}

	private class MyOnClickListener implements OnClickListener{
		@Override
		public void onClick(View v) {
			
			String phoneNum = et_num.getText().toString();
			
			/**
			 * 以下是打电话的核心代码:
			 */
			Intent intent = new Intent();//创建意图对象
			intent.setAction(Intent.ACTION_CALL);//设置意图的动作(拨打电话)
			intent.setData(Uri.parse("tel:" + phoneNum));//设置意图的数据(电话号码)
			
			startActivity(intent);//使用意图开启一个界面(打电话的界面)
			
		}
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

3、string.xml

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

    <string name="app_name">PhoneTest</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>

    <string name="a">请输入电话号码</string>
    <string name="b">拨打号码</string>
    <string name="c">请在这里输入号码</string>
</resources>


4、AndroidManifest.xml

在这个配置文件中加上:

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


---------------------------以上用到的时候点击事件的处理方式的第一种。以下介绍事件处理的第二种、第三种处理方式------------


第二种事件处理方式:

1、MainActivity2

package com.njupt.phonetest;

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

public class MainActivity2 extends Activity implements OnClickListener{

	private EditText et_num;
	private Button bt_call;

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

		et_num = (EditText) findViewById(R.id.num_et);
		bt_call = (Button) findViewById(R.id.call_bt);

		bt_call.setOnClickListener(this);
	}

	public void onClick(View v) {

		String phoneNum = et_num.getText().toString();

		/**
		 * 以下是打电话的核心代码:
		 */
		Intent intent = new Intent();// 创建意图对象
		intent.setAction(Intent.ACTION_CALL);// 设置意图的动作(拨打电话)
		intent.setData(Uri.parse("tel:" + phoneNum));// 设置意图的数据(电话号码)

		startActivity(intent);// 使用意图开启一个界面(打电话的界面)

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

其他的代码不需要改变



第三种事件处理的方式:


1、MainActivity3

package com.njupt.phonetest;

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

public class MainActivity3 extends Activity {

	private EditText et_num;

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

		et_num = (EditText) findViewById(R.id.num_et);
	}

	public void hello(View v) {

		String phoneNum = et_num.getText().toString();

		/**
		 * 以下是打电话的核心代码:
		 */
		Intent intent = new Intent();// 创建意图对象
		intent.setAction(Intent.ACTION_CALL);// 设置意图的动作(拨打电话)
		intent.setData(Uri.parse("tel:" + phoneNum));// 设置意图的数据(电话号码)

		startActivity(intent);// 使用意图开启一个界面(打电话的界面)

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}


2、main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/a"
        />
    
    <EditText 
        android:id="@+id/num_et"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/c"
        />
    
    <Button 
        android:id="@+id/call_bt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/b"
        android:onClick="hello"
        />
</LinearLayout>


源码下载:http://download.csdn.net/detail/caihongshijie6/7600651