Android学习——实现简单的电话拨号
生活随笔
收集整理的這篇文章主要介紹了
Android学习——实现简单的电话拨号
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Android學習——實現簡單的電話撥號
- 新建工程
- 1、編輯界面
- 2、編輯MainActivity
- 3、編輯權限
新建工程
1、編輯界面
設置為線性布局、輸入框提示(輸入類型為數字)、添加圖片按鈕
<?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:id="@+id/activity_main"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="16dp"android:paddingLeft="16dp"android:paddingRight="16dp"android:paddingTop="16dp"android:orientation="vertical"tools:context=".MainActivity"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Call phtone!"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" /><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:hint="輸入電話號碼"android:inputType="number"android:id="@+id/phoneNum"/><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"><ImageButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:background="@mipmap/callphone"android:id="@+id/call_btn"/></RelativeLayout> </LinearLayout>以上代碼中我們設置了文本輸入框(及提示)、圖片按鈕,并給他們設置了各自的id
2、編輯MainActivity
此時我們可以部署試試
撥號界面都正常,但是當我們點擊撥號按鈕時,崩了
我們看看運行過程中的報錯
圖示是有報錯的,紅框中指明是沒有給進權限因此我們進行下一步
3、編輯權限
在AndroidMainfest文件中設置撥號權限
但是在Android6以后,僅僅這樣設置權限是不被認可的
因此有兩種方式
1、在自己手機中通過應用權限賦予軟件權限
2、增加權限申請方法
我們使用第二種方法,
(1)在主類中增加請求
(2)在onCreate中調用
重新運行模擬
進入界面就請求我們是否運行這個撥號權限,我們選擇Allow
我們再通過撥號按鈕就可以成功撥號了
總結
以上是生活随笔為你收集整理的Android学习——实现简单的电话拨号的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DedeCMS网站搭建完整教程
- 下一篇: 跨年烟花但是飘零半生