为了简单起见,仅设置一个按钮
- <?xml version="1.0" encoding="utf-8"?>
- <LinerLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#F3F9F1"
- android:orientation="vertical"
- android:columnCount="4"
- android:collapseColumns=""
- tools:context=".MainActivity">
- <Button
- android:id="@+id/tz"
- android:layout_height="match_parent"
- android:layout_width="match_parent"
- android:text="跳转"
- >
- </Button>
- </LinerLayout>
效果如下:
主页面java代码:
- package com.example.study_taiqiu;
-
- import androidx.appcompat.app.AppCompatActivity;
-
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.Toast;
-
- public class MainActivity extends AppCompatActivity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Button b=findViewById(R.id.tz); //定位到按钮
- b.setOnClickListener(new View.OnClickListener() { //为按钮设置监听器
- @Override
- public void onClick(View view) {
- Intent i=new Intent();
- i.setClass(MainActivity.this,Main_tiaozhuan.class);
- startActivity(i);
- }
- });
- }
- }
跳转页面的布局:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:orientation="vertical" android:layout_width="match_parent"
- android:layout_height="match_parent">
-
- <TextView
- android:id="@+id/textView"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:gravity="center"
- android:text="这是跳转页面"
- android:textSize="30sp" />
- </LinearLayout>
跳转页面java代码:(啥也不用设)
- import android.os.Bundle;
-
-
- import androidx.annotation.Nullable;
- import androidx.appcompat.app.AppCompatActivity;
-
- public class Main_tiaozhuan extends AppCompatActivity {
-
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.tiaozhuan); //加载布局
-
-
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.study_taiqiu">
-
- <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=".Main_tiaozhuan" /> //设置跳转页面
- </application>
-
- </manifest>
首先,其实跳转页面的代码可以简化为:
- package com.example.tiaozhuan;
-
- import androidx.appcompat.app.AppCompatActivity;
-
- import android.content.Intent;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Looper;
- import android.view.View;
- import android.widget.Button;
- import android.widget.Toast;
-
- public class MainActivity extends AppCompatActivity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Button b=findViewById(R.id.tz);
- b.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- startActivity(new Intent(MainActivity.this ,tiaozhuan.class));
- }
- });
- }
- @Override
- protected void onResume() {
- super.onResume();
- new Handler(Looper.myLooper()).postDelayed(next, 3000); //延时3秒,执行页面跳转
- }
-
- private Runnable next =new Runnable() {
- @Override
- public void run() { //页面跳转
- startActivity(new Intent(MainActivity.this ,tiaozhuan.class));
- }
- };
- }