欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android studio案例之實現電話撥號

 更新時間:2021年04月12日 11:26:35   作者:天會晴就會暗  
這篇文章主要介紹了Android studio案例之實現電話撥號,并有詳細的步驟和實現代碼,對此感興趣的同學,可以參考下

 一、代碼配置

1、創(chuàng)建項目

流程看圖

2、增添代碼

更改布局

布局完整代碼

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="電話撥號"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</LinearLayout>

插入圖片

activity_main_xml文件布局

完整代碼

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="電話撥號"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="輸入電話號碼"
        android:inputType="number"
        android:id="@+id/phoneNum"/>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/call"
            android:layout_centerInParent="true" 
            android:id="@+id/call_btn"/>
    </RelativeLayout>
</LinearLayout>

效果展示

mainactivity.java文件

 EditText phoneNum;
    ImageButton call_btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        phoneNum=(EditText) findViewById(R.id.phoneNum);
        call_btn=(ImageButton) findViewById(R.id.call_btn);
       call_btn.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Intent intent=new Intent();
               intent.setAction(Intent.ACTION_CALL);
               intent.setData(Uri.parse("tel:"+phoneNum.getText()));
               startActivity(intent);

圖片

運行代碼

撥號嘗試,出現報錯,需要設置對應權限

3、權限請求

Androidmainfest.xml文件中

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

Android 6.0以上需要自己手動賦予權限。

應用程序權限請求
版本號判斷方法

protected boolean shouldAskPermissions(){
    return (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1);
}

權限申請方法

protected void askPermissions() {
    String[] permissions = {
            "android.permission.CALL_PHONE"
    };
    int requestCode = 200;
    requestPermissions(permissions, requestCode);
}

在onCreate中調用

if(shouldAskPermissions()){
    askPermissions();
}

請求權限加入代碼時要保證程序處于運行狀態(tài),不然代碼加進去會報錯。

二、效果演示

隨便輸入得數字號碼,提示為空號。

以上就是Android studio案例之實現電話撥號的詳細內容,更多關于Android studio電話撥號的資料請關注腳本之家其它相關文章!

相關文章

  • Android串口通信apk源碼詳解(附完整源碼)

    Android串口通信apk源碼詳解(附完整源碼)

    這篇文章主要介紹了Android串口通信apk源碼詳解,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • Android布局技巧之include、merge與ViewStub標簽的巧用

    Android布局技巧之include、merge與ViewStub標簽的巧用

    Android 官方提供了三個用來優(yōu)化布局的標簽,分別是include、merge與ViewStub,下面這篇文章主要給大家介紹了關于Android布局技巧之include、merge與ViewStub標簽巧用的相關資料,需要的朋友可以參考下
    2018-06-06
  • Android超簡單懸浮窗使用教程

    Android超簡單懸浮窗使用教程

    這篇文章主要介紹了Android超簡單懸浮窗使用教程,本文分步驟給大家介紹了使用前需要依賴庫,給大家介紹的非常詳細,需要的朋友可以參考下
    2021-09-09
  • Android HTTP網絡請求的異步實現

    Android HTTP網絡請求的異步實現

    這篇文章主要介紹了Android HTTP網絡請求的異步實現,感興趣的小伙伴們可以參考一下
    2016-07-07
  • Android使用Jsoup解析Html表格的方法

    Android使用Jsoup解析Html表格的方法

    這篇文章主要介紹了Android使用Jsoup解析Html表格的方法,涉及Android中Jsoup的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-12-12
  • Android VideoView類實例講解

    Android VideoView類實例講解

    本文主要介紹Android VideoView類,這里對VideoView類詳細說明了使用方法,以及示例代碼,有興趣的朋友可以參考下,希望能幫助Android 開發(fā)的朋友
    2016-08-08
  • 詳解Android中解析XML的方法

    詳解Android中解析XML的方法

    XML在各種開發(fā)中都廣泛應用,Android也不例外。這篇文章主要介紹了詳解Android中解析XML的方法,有需要的可以了解一下。
    2016-11-11
  • Flutter改變狀態(tài)變量是否必須寫在setState回調詳解

    Flutter改變狀態(tài)變量是否必須寫在setState回調詳解

    這篇文章主要為大家介紹了Flutter改變狀態(tài)變量是否必須寫在setState回調里的原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11
  • Android中okhttp3.4.1+retrofit2.1.0實現離線緩存

    Android中okhttp3.4.1+retrofit2.1.0實現離線緩存

    這篇文章主要介紹了Android中okhttp3.4.1結合retrofit2.1.0實現離線緩存,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • android studio git 刪除已在遠程倉庫的文件或文件夾方式

    android studio git 刪除已在遠程倉庫的文件或文件夾方式

    這篇文章主要介紹了android studio git 刪除已在遠程倉庫的文件或文件夾方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04

最新評論