android编程3-两个activity的跳转

 时间:2026-02-13 23:33:35

1、首先先新建一个Android项目,就叫jump吧,如图:

android编程3-两个activity的跳转

2、准备工作,在layout的文件夹下载新建一个XML,确保现在有连个XML,如图:

android编程3-两个activity的跳转

3、然后修改activity_main.XML的内容,代码如下:

<?xml version="1.0" encoding="utf-8"?>

 <AbsoluteLayout

      android:layout_width="fill_parent"

       android:layout_height="fill_parent" 

        xmlns:android="http://schemas.android.com/apk/res/android" >

         <TextView android:id="@+id/text1" 

             android:textSize="24sp"

             android:layout_width="186px" 

             android:layout_height="186px" 

        android:text="@string/layout1"/>

          <Button android:id="@+id/button1"

               android:layout_width="300px" 

               android:layout_height="wrap_content"

                android:layout_x="50px" 

                android:layout_y="100px"

                 android:text="Go to Layout2">  亲杰 </Button>

                 

          </AbsoluteLayout>

如图:

android编程3-两个activity的跳转

4、然后修改mymain.XML的内容,代码如下:

<?xml version="1.0" encoding="utf-8"?> 

<AbsoluteLayout 

    android:layout_width="fill_parent" 

    android:layout_height="fill_parent"

     xmlns:android="http://schemas.android.com/apk/res/android" > 

     <TextView android:id="@+id/text2" 

         android:textSize="24sp" 

         android:layout_width="186px"

          android:layout_height="186px" 

           篇够android:text="@string/layout2"/> 

           <Button android:id="@+id/button2"

             驼常称   android:layout_width="300px" 

                android:layout_height="wrap_content"

                 android:layout_x="100px" 

                 android:layout_y="82px"

                  android:text="Go to Layout1"> 

                  </Button>

                  

           </AbsoluteLayout>

如图:

android编程3-两个activity的跳转

5、修改主函数代码如下:

package com.example.learn1_4_jump;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) { 

   super.onCreate(savedInstanceState); 

setContentView(R.layout.activity_main); 

Button b1=(Button)findViewById(R.id.button1);

b1.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

jump2();

}

});}

private void jump2() {

// TODO Auto-generated method stub

setContentView(R.layout.mymain);

Button b2=(Button)findViewById(R.id.button2);

b2.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

jump1();

}

});}

 

public void jump1()

{ setContentView(R.layout.activity_main); 

Button b1=(Button)findViewById(R.id.button1); 

b1.setOnClickListener(new Button.OnClickListener() 

{ @Override public void onClick(View v) { 

// TODO Auto-generated method stub

jump2(); } } ); 

}

}

如图:

android编程3-两个activity的跳转

6、运行程序,如图:

android编程3-两个activity的跳转

7、主界面,如图,点击按钮,观察变化,如图:

android编程3-两个activity的跳转

android编程3-两个activity的跳转

  • SAP SD如何分配已更改凭证的输出类型
  • 如何解决发动车时抖动、发动机亮黄灯
  • 豆豉豆皮小炒
  • 如何用photoshop合成女僵尸
  • 如何画绿海龟
  • 热门搜索
    关于防溺水的手抄报 水浒传手抄报 爱我中华手抄报 手抄报内容 六一儿童节的手抄报 手抄报 防溺水的手抄报 手抄报版面设计图 清明手抄报 爱眼护眼手抄报