5.修改MainActivity的布局文件。
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="schemas.android.com/apk/res/and…"
xmlns:app="schemas.android.com/apk/res-aut…"
xmlns:tools="schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开SecondActivity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
6.修改MainActivity。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//实例化按钮
Button button = findViewById(R.id.button);
//设置按钮的点击监听
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//打开SecondActivity
startActivity(new Intent(MainActivity.this,SecondActivity.class));
}
});
}
}
7.效果图。
如果你进阶的路上缺乏方向,可以点击我的【Github】加入我们的圈子和安卓开发者们一起学习交流! 以下全部内容都可以在GitHub中获取!
-
Android进阶学习全套手册
-
Android对标阿里P7学习视频
-
BATJ大厂Android高频面试题
最后,借用我最喜欢的乔布斯语录,作为本文的结尾:
人这一辈子没法做太多的事情,所以每一件都要做得精彩绝伦。 你的时间有限,所以不要为别人而活。不要被教条所限,不要活在别人的观念里。不要让别人的意见左右自己内心的声音。 最重要的是,勇敢的去追随自己的心灵和直觉,只有自己的心灵和直觉才知道你自己的真实想法,其他一切都是次要。