1. 程式人生 > >android studio實現打電話

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();
    }





}