Android学习——实现简单的电话拨号
生活随笔
收集整理的這篇文章主要介紹了
Android学习——实现简单的电话拨号
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Android學(xué)習(xí)——實(shí)現(xiàn)簡(jiǎn)單的電話撥號(hào)
- 新建工程
- 1、編輯界面
- 2、編輯MainActivity
- 3、編輯權(quán)限
新建工程
1、編輯界面
設(shè)置為線性布局、輸入框提示(輸入類型為數(shù)字)、添加圖片按鈕
<?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="輸入電話號(hào)碼"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>以上代碼中我們?cè)O(shè)置了文本輸入框(及提示)、圖片按鈕,并給他們?cè)O(shè)置了各自的id
2、編輯MainActivity
此時(shí)我們可以部署試試
撥號(hào)界面都正常,但是當(dāng)我們點(diǎn)擊撥號(hào)按鈕時(shí),崩了
我們看看運(yùn)行過(guò)程中的報(bào)錯(cuò)
圖示是有報(bào)錯(cuò)的,紅框中指明是沒(méi)有給進(jìn)權(quán)限因此我們進(jìn)行下一步
3、編輯權(quán)限
在AndroidMainfest文件中設(shè)置撥號(hào)權(quán)限
但是在Android6以后,僅僅這樣設(shè)置權(quán)限是不被認(rèn)可的
因此有兩種方式
1、在自己手機(jī)中通過(guò)應(yīng)用權(quán)限賦予軟件權(quán)限
2、增加權(quán)限申請(qǐng)方法
我們使用第二種方法,
(1)在主類中增加請(qǐng)求
(2)在onCreate中調(diào)用
重新運(yùn)行模擬
進(jìn)入界面就請(qǐng)求我們是否運(yùn)行這個(gè)撥號(hào)權(quán)限,我們選擇Allow
我們?cè)偻ㄟ^(guò)撥號(hào)按鈕就可以成功撥號(hào)了
總結(jié)
以上是生活随笔為你收集整理的Android学习——实现简单的电话拨号的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: DedeCMS网站搭建完整教程
- 下一篇: 跨年烟花但是飘零半生