第四十二篇----拨打电话

时间:2019-03-21
本文章向大家介绍第四十二篇----拨打电话,主要包括第四十二篇----拨打电话使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

界面:

MainActivity.java

package com.example.aimee.phone;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    private Button mBtPhone;
    private EditText mEtPhone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mBtPhone = findViewById(R.id.button);
        mEtPhone = (EditText) findViewById(R.id.editview);
        mBtPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + mEtPhone.getText()));
                
                //权限设置
                if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
                startActivity(intent);
            }
        });
    }
}
View Code

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/phonetext" />

    <EditText
        android:id="@+id/editview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/phonenumber"
        app:layout_constraintTop_toBottomOf="@+id/textview"/>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/callphone"
        app:layout_constraintTop_toBottomOf="@+id/editview"/>

</android.support.constraint.ConstraintLayout>
View Code

最后,不要忘记在清单文件中添加权限

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

小提示

1.可以自己设置快捷输入方式.

Settings-->Editor-->Live Templates,查看常用语句的快捷输入法,比如output里面的sout,当你在代码中输入sout按回车,那么就会自动补全System.out.println();如果点击右边的加号可以自己定义输入快捷方式。

2.常用快捷键Alt+Enter

一般代码飘红,可以尝试使用Alt+Enter

比如MainActivity代码中本来没有设置权限那一组代码的,由于startActivity中intent飘红,将鼠标定位到intent那儿,按下Alt+Enter就会自动出现添加权限的代码。

另外,输入(Button)你打印find会出现findViewById();然后填入R.id.button后将鼠标定位到括号后面,Alt+Enter,会自动补全成Button button = (Button) findViewById(R.id.button);此时你可以更换button名字,或者直接按enter就行了。如果你想要Button button在onCreate外面定义,可以按Ctrl+alt+f,它就会自动生成变量。