android程式設計3-兩個activity的跳轉?

Tags: 按鈕, 過程,

接下我要學習的是兩個activity的跳轉通過按鈕響應,實現這過程

工具/原料

win7系統

adt-bundle-windows

方法/步驟

首先先新建一個Android專案,就叫jump吧,如圖:

android程式設計3-兩個activity的跳轉

準備工作,在layout的資料夾下載新建一個XML,確保現在有連個XML,如圖:

android程式設計3-兩個activity的跳轉

然後修改activity_main.XML的內容,程式碼如下:

android:layout_width="fill_parent"

android:layout_height="fill_parent"

xmlns:android="; >

android:textSize="24sp"

android:layout_width="186px"

android:layout_height="186px"

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

如圖:

android程式設計3-兩個activity的跳轉

然後修改mymain.XML的內容,程式碼如下:

android:layout_width="fill_parent"

android:layout_height="fill_parent"

xmlns:android="; >

android:textSize="24sp"

android:layout_width="186px"

android:layout_height="186px"

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

如圖:

android程式設計3-兩個activity的跳轉

修改主函式程式碼如下:

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的跳轉

執行程式,如圖:

android程式設計3-兩個activity的跳轉

主介面,如圖,點選按鈕,觀察變化,如圖:

android程式設計3-兩個activity的跳轉

android程式設計3-兩個activity的跳轉

注意事項

本人原創,如對您有幫助,勞煩點個贊

按鈕, 過程,
相關問題答案