1、在layout下创建一个skip_layout.xml文件:(该文件用来测试页面跳转)
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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" tools:context=".SkipActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="跳转的界面!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
2、编写SkipActivity.java文件,修改启动的界面为skip_layout
package com.example.togglepages; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; public class SkipActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.skip_layout); } }
3、编辑好SkipActivity.java文件后,要在AndroidMainifest.xml里面注册SkipActivity,注册的代码如下
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.togglepages"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".SkipActivity"> </activity> </application> </manifest>
4、在layout下创建begin_layout.xml并且添加按钮等组件,用来实现点击后跳转界面。
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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:background="@mipmap/b" tools:context=".MainActivity"> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="40dp" android:layout_marginBottom="33dp" android:text="按钮2" android:background="#1c97f5" app:layout_constraintBottom_toTopOf="@+id/button2" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="110dp" android:text="按钮1" android:background="#1c97f5" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="@+id/btn" app:layout_constraintTop_toBottomOf="@+id/btn" /> </androidx.constraintlayout.widget.ConstraintLayout>
5、添加了按钮后,修改MainActivity.java文件,在onCreate中获取刚刚在界面文件中添加的按钮,并且实现点击事件监听。并在事件触发处理函数中添加界面跳转的代码,实现界面跳转使用了Intent,具体的代码如下
package com.example.togglepages; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn=findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ Intent it=new Intent(); it.setClass(MainActivity.this,SkipActivity.class); MainActivity.this.startActivity(it); } }); } }