Android Studio三种显示Intent的方式和隐式Intent方式

一、三种显示意图

(一)实现代码

三种方式中的前两种都是打开当前activity的界面,第三种是打开另一个APP的activity

  • 新建两个activity,命名为DateActivity和ClockActivity,工程目录结构如下
    在这里插入图片描述
  • 选择activity_main.xml文件,添加三个button控件
    时钟button控件
    <Button
        android:id="@+id/Clock_Btn"
        android:layout_width="275dp"
        android:layout_height="wrap_content"
        android:layout_x="69dp"
        android:layout_y="152dp"
        android:text="方式一"
        android:textSize="35sp"
        tools:layout_editor_absoluteX="57dp"
        tools:layout_editor_absoluteY="145dp" />

日历button控件

<Button
        android:id="@+id/Calendar_Btn"
        android:layout_width="275dp"
        android:layout_height="wrap_content"
        android:layout_x="71dp"
        android:layout_y="281dp"
        android:text="方式二"
        android:textSize="35sp"
        tools:layout_editor_absoluteX="57dp"
        tools:layout_editor_absoluteY="317dp" />

跳转其它APP的控件

<Button
        android:id="@+id/actother_Btn"
        android:layout_width="275dp"
        android:layout_height="wrap_content"
        android:layout_x="71dp"
        android:layout_y="412dp"
        android:text="方式三"
        android:textSize="35sp"
        tools:layout_editor_absoluteX="57dp"
        tools:layout_editor_absoluteY="502dp" />

采用绝对布局
在这里插入图片描述

  • 设计布局
    在这里插入图片描述
  • 添加时间控件
    在这里插入图片描述
  • 设计布局
    在这里插入图片描述
  • 添加日期控件
    在这里插入图片描述
  • 设计布局
    在这里插入图片描述
  • 主界面的编写
    实现OnClick接口
public class MainActivity extends AppCompatActivity implements View.OnClickListener{

先定义按钮

Button type1_btn;
Button type2_btn;
Button type3_btn;

实例化三个按钮

type1_btn= (Button) findViewById(R.id.Clock_Btn);
type2_btn= (Button) findViewById(R.id.Calendar_Btn);
type3_btn= (Button) findViewById(R.id.actother_Btn);

监听器

type1_btn.setOnClickListener(this);
type2_btn.setOnClickListener(this);
type3_btn.setOnClickListener(this);

点击方法

public void onClick(View v) {
        Intent intent=new Intent();
        switch (v.getId()){
            case R.id.Clock_Btn:     //方式一
                intent.setClass(this,ClockActivity.class);
                break;
            case R.id.Calendar_Btn:   //方式二
                intent.setClassName(this,"com.example.app2.DateActivity"); //要打开的全类名
                break;
            case R.id.actother_Btn:   //方式三
                intent.setClassName("com.example.hello","com.example.hello.MainActivity");   //要打开的项目名,要打开项目的全类名
                break;

        }
        startActivity(intent);
    }

(二)实现效果

在这里插入图片描述

二、隐式意图

一个没有声明intent-Filter的组件只能响应指明自己名字的显示意图请求,而无法响应隐式意图请求,而一个声明了intent-Filter的组件既可以相应显示意图请求,也可以响应隐式意图请求。
Android系统会根据隐式意图中设置的动作(action)、类别(category)、数据(URI和数据类别)找到最合适的组件来处理这个意图
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值