Android中級教程(一)之—-手機頁面的轉換setContentView的應用

來源:互聯網
上載者:User

     大家好,我們這一節講的是手機頁面的轉換setContentView
的應用.在網頁的世界裡,想要在兩個頁面間的轉換,只要利用超連結就可以實現,

但是在手機的世界裡,要如何?手機頁面的轉換呢? 最簡單的方法就是改變Activity
Layout
!

 

    在這個例子中,將布局兩個Layout
,分別為Layout1(main.xml)
Layout2(mylayout.xml),
預設的Layout
main.xml,
我們在Layout1
當中建立一個按鈕,當單擊按鈕時,顯示第二個Layout(mylayout.xml)
;同樣地,在Layout2
裡也設計一個按鈕,當單擊第二個Layout
的按鈕之後,剛顯示回原來的Layout1
,現在就來示範如何在兩個頁面之間互相切換.

 

首先看一下(為了區別兩個Layout
,我們分別設定了不同的背景色):

 

 


 

 

 

下面是我們本程式所涉及的相關代碼,首先是主介面布局main.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="歡迎來到魏祝林的部落格"
    />
<Button
    android:id="@+id/bt1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="點擊進入Layout2"
/>


</LinearLayout>

 

其次我們在main.xml
同一目錄建立一個為mylayout.xml
檔案,代碼如下:

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffffff"

    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Welcome to Mr Wei's blog"
    />
<Button
    android:id="@+id/bt2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="點擊進入Layout1"
/>


</LinearLayout>

 

最後是我們的核心程式setContentViewDemo.java

 

package com.android.setContentViewDemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

import android.widget.Button;

public class setContentViewDemo extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 載入main.xml Layout
        setContentView(R.layout.main);


        // 以findViewById()取得Button對象並添加事件onClickLisener
        Button bt1 = (Button) findViewById(R.id.bt1);
        bt1.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
               

goToLayout2();
            }
        });
    }

    // 將layout由main.xml切換成mylayout.xml
    public void goToLayout2() {
        // 將layout改成mylayout
        setContentView(R.layout.mylayout);
        Button b2 = (Button) findViewById(R.id.bt2);
        b2.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                goToLayout1();
            }
        });
    }

    // 將layout由mylayout.xml切換成main.xml
    public void goToLayout1() {
        setContentView(R.layout.main);
        Button bt1 = (Button) findViewById(R.id.bt1);
        bt1.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                goToLayout2();
            }
        });
    }


}

 

最後執行之!,這一節就到此結束~

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.