Android使用Intent的Action和Data属性实现点击按钮跳转到拨打电话和发送短信界面
作者:霸道流氓 发布时间:2021-09-04 15:39:22
标签:android,按钮,跳转,data,属性
场景
点击拨打电话按钮,跳转到拨打电话页面
点击发送短信按钮,跳转到发送短信页面
注:
实现
将布局改为LinearLayout,并通过android:orientation="vertical">
设置为垂直布局,然后添加id属性。
然后添加两个按钮,并设置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="发送短信"
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的点击事件 * 差不多,所有抽离出一个公共的点击事件 * 对象。
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
//将view强转为Button
Button button = (Button) v;
//根据button的id
switch(button.getId()){
//如果是拨打电话按钮
case R.id.call:
//设置Action行为属性
intent.setAction(intent.ACTION_DIAL);
//设置数据 后面123456789是默认要拨打的电话
intent.setData(Uri.parse("tel:123456789"));
startActivity(intent);
break;
case R.id.send:
//设置行为为 发送短信
intent.setAction(intent.ACTION_SENDTO);
//设置发送至 10086
intent.setData(Uri.parse("smsto:10086"));
//设置短信的默认发送内容
intent.putExtra("sms_body","公众号:霸道的程序猿");
startActivity(intent);
break;
}
}
};
然后在OnCreate中对按钮设置点击事件 * 。
完整示例代码
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强转为Button
Button button = (Button) v;
//根据button的id
switch(button.getId()){
//如果是拨打电话按钮
case R.id.call:
//设置Action行为属性
intent.setAction(intent.ACTION_DIAL);
//设置数据 后面123456789是默认要拨打的电话
intent.setData(Uri.parse("tel:123456789"));
startActivity(intent);
break;
case R.id.send:
//设置行为为 发送短信
intent.setAction(intent.ACTION_SENDTO);
//设置发送至 10086
intent.setData(Uri.parse("smsto:10086"));
//设置短信的默认发送内容
intent.putExtra("sms_body","公众号:霸道的程序猿");
startActivity(intent);
break;
}
}
};
}
因为用到了打电话和发动短信,所以需要声明这两个权限,打开AndroidMainfest.xml
<!--添加打电话权限-->
<uses-permission android:name="android.permission.CALL_PHONE"/>
<!--添加发送短信权限-->
<uses-permission android:name="android.permission.SEND_SMS"/>
总结
以上所述是小编给大家介绍的Android使用Intent的Action和Data属性实现点击按钮跳转到拨打电话和发送短信界面网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
来源:https://www.cnblogs.com/badaoliumangqizhi/archive/2020/01/08/12168987.html


猜你喜欢
- 前言很长一段时间没写博客了,再不写点东西真说不过去,把工作上的一些有价值的东西整理出来分享,在当下还有点时效性,不然迟早会烂在肚子里的。还记
- 声明:本文中资源全部收集整理于网络并无偿提供,仅可用于个人学习交流;请勿转载、售卖或商用;侵权联删!免责声明:本教程所有资源均来源于网络;仅
- instanceof 严格来说是Java中的一个双目运算符,用来测试一个对象是否为一个类的实例,用法为:boolean result = o
- 大家在银行交易某些业务时,都可以看到无论是身份证、银行账号中间部分都是用*号替换的,下面小编把代码整理如下:/// <summary&
- 问题在Android开发中,遇到一个问题,是ListView嵌套GridView,需要点击整个ListView的Item进行跳转。但是在点击
- ServletContext 基础知识获取 ServletContext对象有两种方式可以获取:使用 servletconfig 对象获取使
- 在项目中,我们要求做一个纸飞机的功能:就是当打开这个界面时,会有4架纸飞机从屏幕左侧飞入,然后到达自己的位置坐上下浮动,同时云彩也不断地从屏
- 需要导入ant.jar包,apache网站(http://ant.apache.org/bindownload.cgi)下载即可。impor
- 在上一篇文章中,我们之前对BarChart.lerp的定义并不是高效的,我们正在创建的Bar实例,仅作为Bar.lerp的参数给出,并且针对
- 导入maven依赖<!-- https://mvnrepository.com/artifact/com.fasterxml.jack
- 在ios手机上经常看到页面上下滑动回弹效果,安卓中没有原生控件支持,这里自己就去自定义一个scrollview实现回弹效果1. 新建MySc
- 本文实例为大家分享了Spring boot多线程配置的具体代码,供大家参考,具体内容如下1、配置线程配置类package test;impo
- 本文实例讲述了C#判断字符串是否存在字母及字符串中字符的替换的方法。分享给大家供大家参考。具体实现方法如下:首先要添加对命名空间“using
- 本文实例为大家分享了java实现登录验证码功能的具体代码,供大家参考,具体内容如下登录验证码登录验证是大多数登录系统都会用到的一个功能,它的
- 正在编译...1>Ipv4IPv6traceroutesrc.cpp1>d:\研究生\c++\study\test\test\i
- TIOBE 11 月编程语言排行榜,Python 逆袭C#曾经有一段时间,脚本语言因其易于编写和易于运行的特性,被预测在未来将发展强大。因此
- 今天有个项目需要使用redis,并且有使用脚本的需求。但是因为之前没有写过,所以还有一点点不熟悉,今天记录一下。原因:原子操作,redis会
- java对字符串进行utf-8编码我们在调用第三方 API 时,常常会被要求用到路径变量,而路径变量一般都是 utf-8 编码的,因此需要对
- 一、简介1.1 Log4.net优点几乎所有的大型应用都会有自己的用于跟踪调试的API。因为一旦程序被部署以后,就不太可能再利用专门的调试工
- 前提:你的电脑是AMD处理器,想使用Android studio,自己的电脑系统是win10家庭版,在百度找到勾选hyper-v就能用,然后