(1)项目结构如下:
(2)MainActivity.java
package com.example.phonecall; 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.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { private Button button; private EditText editText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) this.findViewById(R.id.button1); editText = (EditText) this.findViewById(R.id.editText1); button.setOnClickListener(new ButtonClickListener()); } private final class ButtonClickListener implements View.OnClickListener { @Override public void onClick(View v) { String number = editText.getText().toString(); Intent intent = new Intent(); /* * 拨号的话要在清单文件中添加拨号权限 */ intent.setAction("android.intent.action.CALL"); intent.addCategory("android.intent.category.DEFAULT"); intent.setData(Uri.parse("tel:" + number)); 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; } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="请输入你的手机号" /> <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="拨号" /> </LinearLayout>
清单文件中加入权限:
<uses-permission android:name="android.permission.CALL_PHONE" />
原文地址:http://blog.csdn.net/xlgen157387/article/details/43985953