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

Android使用Intent的Action和Data屬性實現(xiàn)點擊按鈕跳轉(zhuǎn)到撥打電話和發(fā)送短信界面

 更新時間:2020年01月10日 09:55:22   作者:霸道流氓  
這篇文章主要介紹了Android中使用Intent的Action和Data屬性實現(xiàn)點擊按鈕跳轉(zhuǎn)到撥打電話和發(fā)送短信,需要的朋友可以參考下

場景

點擊撥打電話按鈕,跳轉(zhuǎn)到撥打電話頁面

點擊發(fā)送短信按鈕,跳轉(zhuǎn)到發(fā)送短信頁面

注:

實現(xiàn)

將布局改為LinearLayout,并通過android:orientation="vertical">設(shè)置為垂直布局,然后添加id屬性。

然后添加兩個按鈕,并設(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獲取者兩個Button

 Button buttonCall = (Button) findViewById(R.id.call);
    Button buttonSend = (Button) findViewById(R.id.send);

又因為這兩個Button的點擊事件監(jiān)聽器差不多,所有抽離出一個公共的點擊事件監(jiān)聽器對象。

View.OnClickListener listener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      Intent intent = new Intent();
      //將view強轉(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是默認要撥打的電話
          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è)置短信的默認發(fā)送內(nèi)容
          intent.putExtra("sms_body","公眾號:霸道的程序猿");
          startActivity(intent);
          break;
      }
    }
  };

然后在OnCreate中對按鈕設(shè)置點擊事件監(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強轉(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是默認要撥打的電話
          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è)置短信的默認發(fā)送內(nèi)容
          intent.putExtra("sms_body","公眾號:霸道的程序猿");
          startActivity(intent);
          break;
      }
    }
  };
}

因為用到了打電話和發(fā)動短信,所以需要聲明這兩個權(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"/>

總結(jié)

以上所述是小編給大家介紹的Android使用Intent的Action和Data屬性實現(xiàn)點擊按鈕跳轉(zhuǎn)到撥打電話和發(fā)送短信界面,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!

相關(guān)文章

最新評論