三三要成为安卓糕手
默认重写onCreate,继承关系,最后的父类是Activity
manifest [ˈmænɪfest] 清单
包含MainActivity对应的xml布局文件和清单文件
activity要被识别的话,需要先在清单文件中进行声明
android.intent.action.MAIN
和 android.intent.category.LAUNCHER
的 Activity
才会作为应用的主入口 ,也就是点击应用图标最先启动的页面。
其实很简单,创建.java类,继承AppCompatActivity
类重写onCreate方法,定义一个xml布局,并关联;在清单文件中声明这个类就ok了
定义一个按钮作为媒介
<Button
android:id="@+id/btn_second"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="跳转到Second页面"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
之前监听器的写法用的多一点,这次解锁新技能;实现View.OnClickListener接口,重写onClick方法,找到按钮控件传入this参数即可,这样写的好处,代码更简洁
public class MainActivity extends AppCompatActivity implements View.OnClickListener {}
Button btnStartSecond = findViewById(R.id.btn_second);
可以这么勇的本质原因点击监听器是一个接口
有多个按钮,代码的复用率就上去了
<Button
android:id="@+id/btn_second2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="跳转到Second2页面"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/btn_second"/>
Button btnStartSecond = findViewById(R.id.btn_second);
btnStartSecond.setOnClickListener(this);
Button btnStartSecond2 = findViewById(R.id.btn_second2);
btnStartSecond2.setOnClickListener(this);
Log.i(TAG, "onCreate: btnStartSecond id = " + btnStartSecond.getId());
Log.i(TAG, "onCreate: btnStartSecond2 id = " + btnStartSecond2.getId());
@Override
public void onClick(View v) {
int id = v.getId();
Log.i(TAG, "onClick: v id = " + id);
if (id == R.id.btn_second) {
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
} else if (id == R.id.btn_second2) {
Intent intent = new Intent();
intent.setAction("com.is.acbjp.SecondActivity");
// intent.addCategory("android.intent.category.DEFAULT");
intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivity(intent);
}
注:OnClick方法中不建议使用switch语句,跟底层声明有关,建议我们使用if
intent可以用来启动activity,翻译成意图
intent这玩意有点nb,是用来相互通信,相互传递一些信息的中介
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
Intent intent = new Intent();
intent.setAction("com.is.acbjp.SecondActivity");
// intent.addCategory("android.intent.category.DEFAULT");
intent.addCategory(Intent.CATEGORY_DEFAULT);//没有这句代码也无所谓,系统会自动添加的
startActivity(intent);
需要再清单中进行声明;安卓中提供了一些隐式启动的类型,android.intent.category.DEFAULT表示默认,90%以上的需求用DEFAULT就可以了;EMAIL和MAPS也有在用
<activity
android:name="com.xlong.androidcomponentbyjavaproject.SecondActivity"
android:exported="true">
<intent-filter>
<action android:name="com.is.acbjp.SecondActivity"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
intent-filter 意图过滤启动器
action中的name属性:一个自定义的字符串
category 英 [ˈkætəɡəri] 类别 中的name属性:默认即可
//这两句代码等价
intent.addCategory("android.intent.category.DEFAULT");
intent.addCategory(Intent.CATEGORY_DEFAULT
**btn_second2
**按钮的点击事件:
通过setAction("com.is.acbjp.SecondActivity")
指定动作
配合addCategory(Intent.CATEGORY_DEFAULT)
系统会根据这两个条件去匹配AndroidManifest.xml
中对应intent-filter
的组件
这样就完成了使用了隐式跳转
对比项 | 显式意图(Explicit Intent) | 隐式意图(Implicit Intent) |
---|---|---|
目标明确性 | 明确指定组件类名(如SecondActivity.class) | 不指定类名,通过动作 / 数据等描述需求 |
适用范围 | 主要用于应用内部组件 | 主要用于跨应用组件调用,或内部解耦 |
依赖配置 | 不需要在AndroidManifest.xml中配置intent-filter | 必须在AndroidManifest.xml中配置intent-filter,否则无法被匹配 |
灵活性 | 耦合度高(直接依赖类名) | 耦合度低(通过 “动作” 通信,不依赖具体类名) |