Android studio案例之實(shí)現(xiàn)電話撥號(hào)
一、代碼配置
1、創(chuàng)建項(xià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="電話撥號(hào)" 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="電話撥號(hào)" 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="輸入電話號(hào)碼" 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);
圖片
運(yùn)行代碼
撥號(hào)嘗試,出現(xiàn)報(bào)錯(cuò),需要設(shè)置對(duì)應(yīng)權(quán)限
3、權(quán)限請(qǐng)求
Androidmainfest.xml文件中
<uses-permission android:name="android.permission.CALL_PHONE"/>
Android 6.0以上需要自己手動(dòng)賦予權(quán)限。
應(yīng)用程序權(quán)限請(qǐng)求
版本號(hào)判斷方法
protected boolean shouldAskPermissions(){ return (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1); }
權(quán)限申請(qǐng)方法
protected void askPermissions() { String[] permissions = { "android.permission.CALL_PHONE" }; int requestCode = 200; requestPermissions(permissions, requestCode); }
在onCreate中調(diào)用
if(shouldAskPermissions()){ askPermissions(); }
請(qǐng)求權(quán)限加入代碼時(shí)要保證程序處于運(yùn)行狀態(tài),不然代碼加進(jìn)去會(huì)報(bào)錯(cuò)。
二、效果演示
隨便輸入得數(shù)字號(hào)碼,提示為空號(hào)。
以上就是Android studio案例之實(shí)現(xiàn)電話撥號(hào)的詳細(xì)內(nèi)容,更多關(guān)于Android studio電話撥號(hào)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android布局技巧之include、merge與ViewStub標(biāo)簽的巧用
Android 官方提供了三個(gè)用來(lái)優(yōu)化布局的標(biāo)簽,分別是include、merge與ViewStub,下面這篇文章主要給大家介紹了關(guān)于Android布局技巧之include、merge與ViewStub標(biāo)簽巧用的相關(guān)資料,需要的朋友可以參考下2018-06-06Android HTTP網(wǎng)絡(luò)請(qǐng)求的異步實(shí)現(xiàn)
這篇文章主要介紹了Android HTTP網(wǎng)絡(luò)請(qǐng)求的異步實(shí)現(xiàn),感興趣的小伙伴們可以參考一下2016-07-07Flutter改變狀態(tài)變量是否必須寫(xiě)在setState回調(diào)詳解
這篇文章主要為大家介紹了Flutter改變狀態(tài)變量是否必須寫(xiě)在setState回調(diào)里的原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11Android中okhttp3.4.1+retrofit2.1.0實(shí)現(xiàn)離線緩存
這篇文章主要介紹了Android中okhttp3.4.1結(jié)合retrofit2.1.0實(shí)現(xiàn)離線緩存,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10android studio git 刪除已在遠(yuǎn)程倉(cāng)庫(kù)的文件或文件夾方式
這篇文章主要介紹了android studio git 刪除已在遠(yuǎn)程倉(cāng)庫(kù)的文件或文件夾方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04