這篇文章主要介紹了怎么在Android中利用Intent實(shí)現(xiàn)一個(gè)點(diǎn)擊按鈕跳轉(zhuǎn)到撥打電話和發(fā)送短信界面,此處通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考價(jià)值,需要的朋友可以參考下:
創(chuàng)新互聯(lián)公司網(wǎng)站建設(shè)公司,提供成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì),網(wǎng)頁設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);可快速的進(jìn)行網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,是專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
Android是一種基于Linux內(nèi)核的自由及開放源代碼的操作系統(tǒng),主要使用于移動(dòng)設(shè)備,如智能手機(jī)和平板電腦,由美國(guó)Google公司和開放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開發(fā)。
實(shí)現(xiàn)
將布局改為L(zhǎng)inearLayout,并通過android:orientation="vertical">
設(shè)置為垂直布局,然后添加id屬性。
然后添加兩個(gè)按鈕,并設(shè)置Id屬性與顯示文本。
<?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=".IntentActivity"> <Button android:id="@+id/call" android:text="撥打電話" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:id="@+id/send" android:text="發(fā)送短信" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
然后來到Activity,首先通過ID獲取者兩個(gè)Button
Button buttonCall = (Button) findViewById(R.id.call); Button buttonSend = (Button) findViewById(R.id.send);
又因?yàn)檫@兩個(gè)Button的點(diǎn)擊事件監(jiān)聽器差不多,所有抽離出一個(gè)公共的點(diǎn)擊事件監(jiān)聽器對(duì)象。
View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); //將view強(qiáng)轉(zhuǎn)為Button Button button = (Button) v; //根據(jù)button的id switch(button.getId()){ //如果是撥打電話按鈕 case R.id.call: //設(shè)置Action行為屬性 intent.setAction(intent.ACTION_DIAL); //設(shè)置數(shù)據(jù) 后面123456789是默認(rèn)要撥打的電話 intent.setData(Uri.parse("tel:123456789")); startActivity(intent); break; case R.id.send: //設(shè)置行為為 發(fā)送短信 intent.setAction(intent.ACTION_SENDTO); //設(shè)置發(fā)送至 10086 intent.setData(Uri.parse("smsto:10086")); //設(shè)置短信的默認(rèn)發(fā)送內(nèi)容 intent.putExtra("sms_body","公眾號(hào):霸道的程序猿"); startActivity(intent); break; } } };
然后在OnCreate中對(duì)按鈕設(shè)置點(diǎn)擊事件監(jiān)聽器。
完整示例代碼
package com.badao.relativelayouttest; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.Button; public class IntentActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_intent); Button buttonCall = (Button) findViewById(R.id.call); Button buttonSend = (Button) findViewById(R.id.send); buttonCall.setOnClickListener(listener); buttonSend.setOnClickListener(listener); } View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); //將view強(qiáng)轉(zhuǎn)為Button Button button = (Button) v; //根據(jù)button的id switch(button.getId()){ //如果是撥打電話按鈕 case R.id.call: //設(shè)置Action行為屬性 intent.setAction(intent.ACTION_DIAL); //設(shè)置數(shù)據(jù) 后面123456789是默認(rèn)要撥打的電話 intent.setData(Uri.parse("tel:123456789")); startActivity(intent); break; case R.id.send: //設(shè)置行為為 發(fā)送短信 intent.setAction(intent.ACTION_SENDTO); //設(shè)置發(fā)送至 10086 intent.setData(Uri.parse("smsto:10086")); //設(shè)置短信的默認(rèn)發(fā)送內(nèi)容 intent.putExtra("sms_body","公眾號(hào):霸道的程序猿"); startActivity(intent); break; } } }; }
因?yàn)橛玫搅舜螂娫捄桶l(fā)動(dòng)短信,所以需要聲明這兩個(gè)權(quán)限,打開AndroidMainfest.xml
<!--添加打電話權(quán)限--> <uses-permission android:name="android.permission.CALL_PHONE"/> <!--添加發(fā)送短信權(quán)限--> <uses-permission android:name="android.permission.SEND_SMS"/>
到此這篇關(guān)于怎么在Android中利用Intent實(shí)現(xiàn)一個(gè)點(diǎn)擊按鈕跳轉(zhuǎn)到撥打電話和發(fā)送短信界面的文章就介紹到這了,更多相關(guān)怎么在Android中利用Intent實(shí)現(xiàn)一個(gè)點(diǎn)擊按鈕跳轉(zhuǎn)到撥打電話和發(fā)送短信界面的內(nèi)容請(qǐng)搜索創(chuàng)新互聯(lián)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持創(chuàng)新互聯(lián)!
分享名稱:怎么在Android中利用Intent實(shí)現(xiàn)一個(gè)點(diǎn)擊按鈕跳轉(zhuǎn)到撥打電話和發(fā)送短信界面
文章轉(zhuǎn)載:http://m.newbst.com/article18/jesjgp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、網(wǎng)站改版、品牌網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計(jì)、動(dòng)態(tài)網(wǎng)站、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)