【Android】实现两个界面切换跳转(两个Activity,两个XML之间的来回切换)

在安卓中最常见的就是按下按钮后跳转到另一个界面,关于界面的跳转有两种方法:

方法1:

一个Activity,两个XML文件之间的页面切换,详情可见

https://blog.csdn.net/yao_yaoya/article/details/127788317

方法2:

两个Activity,两个XML文件之间使用Intent实现来回跳转。

1.首先我们要创建两个XML文件,分别取名为activity_main.xml和activity_main2.xml。

(1)activity_main.xml:

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        >
        <TextView
            android:id="@+id/tv1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="30sp"
            android:gravity="center"
            android:text="这里是第一页"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btn1"
            android:textSize="30sp"
            android:layout_centerHorizontal="true"
            android:layout_below="@+id/tv1"
            android:text="点击我跳转到第二页"/>
    </RelativeLayout>

 (2)activity_main2:

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center">
        <TextView
            android:id="@+id/tv2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="30sp"
            android:gravity="center"
            android:text="这里是第二页"/>
        <Button
            android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tv2"
            android:layout_centerHorizontal="true"
            android:textSize="30sp"
            android:text="点击返回第一页"/>
    </RelativeLayout>

 2.在JAVA文件中写两个界面的切换,创建MainActivity.java和MainActivity2.java文件:

(1)MainActivity.java:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = this.findViewById(R.id.btn1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,MainActivity2.class);
                startActivity(intent);
            }
        });
    }
}

(2)MainActivity2.java:

public class MainActivity2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        Button button = this.findViewById(R.id.btn2);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}

简单粗暴!

感谢Thanks♪(・ω・)ノ!!!

  • 5
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
Android中,要实现两个XML页面之间,您需要执行以下步骤: 1. 在AndroidManifest.xml文件中声明所有活动(Activity) 在AndroidManifest.xml文件中添加所有活动的声明(Activity),即包括源活动和目标活动。例如: ``` <activity android:name=".MainActivity" /> <activity android:name=".SecondActivity" /> ``` 2. 在源活动(即第一个XML页面)中添加按钮 在源活动的XML布局文件中添加一个按钮。例如: ``` <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Go to Second Activity" /> ``` 3. 在源活动的Java类中添加按钮单击事件处理程序 在源活动的Java类中添加按钮单击事件处理程序,以便在单击按钮时执行操作。例如: ``` Button button = findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(MainActivity.this, SecondActivity.class); startActivity(intent); } }); ``` 4. 在目标活动(即第二个XML页面)中添加布局 在目标活动的XML布局文件中添加所需的布局。例如: ``` <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is Second Activity" /> ``` 5. 运行应用程序 通过运行应用程序来测试操作。在单击源活动中的按钮后,应该会打开目标活动。 希望这可以帮助您在Android实现两个XML页面之间

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yao_yao_ya

感谢打赏ლ(°◕‵ƹ′◕ლ)

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值