android studio实现打电话

首先需要在Manifest中配置权限 

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

main.xml代码如下


android中有6中布局方式 TabLayout,RelativeLayou ,AbsoluteLayout,LinerLayout,GridLayout,FrameLayout


xml version="1.0" encoding="utf-8"?>
<android.widget.RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="app.yu.com.test.Main">

    

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请输入电话号码"
        tools:layout_editor_absoluteY="20dp"
        tools:layout_editor_absoluteX="0dp"
        tools:ignore="MissingConstraints" />

    <EditText
        android:id="@+id/edit"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
         />

    <Button
        android:id="@+id/bt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="拨打电话"
        android:layout_alignParentTop="false"

        android:layout_marginTop="80dp"
        />







android.widget.RelativeLayout>
 
  
main.java中的代码如下
 
  
package app.yu.com.test;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class Main extends AppCompatActivity implements View.OnClickListener {

    EditText editText;

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

        Button btn = (Button)this.findViewById(R.id.bt1);

        btn.setOnClickListener(this);

        editText = (EditText)findViewById(R.id.edit);
    }

    @Override
    public void onClick(View v) {

        Intent intent = new Intent();

        intent.setAction(Intent.ACTION_CALL);

        intent.setData(Uri.parse("tel:"+ editText.getText()));

        startActivity(intent);


        Toast.makeText(getApplicationContext(),"实现点击第三个按钮",Toast.LENGTH_LONG).show();
    }





}


你可能感兴趣的:(Android)