下面是一个简单的打电话的功能
<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"
>
<EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:textSize="25dp" />
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打电话"
android:textSize="25dp" />
</LinearLayout>
package com.example.call;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView textview=(TextView) findViewById(R.id.edittext);
Button button=(Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String number=textview.getText().toString();
Intent intent =new Intent();
//设置动作
intent.setAction(Intent.ACTION_CALL);
//设置数据
intent.setData(Uri.parse("tel:"+textview));
startActivity(intent);
}
});
}
最重要的一点:必须在AndroidManifest.xml,添加拨打电话的权限:
不加这条权限不能打电话