以文本方式查看主題

-  曙海教育集團論壇  (http://www.scb-ycwb.com/bbs/index.asp)
--  Android應用開發  (http://www.scb-ycwb.com/bbs/list.asp?boardid=45)
----  Android中級教程  (http://www.scb-ycwb.com/bbs/dispbbs.asp?boardid=45&id=2020)

--  作者:wangxinxin
--  發布時間:2010-12-3 11:06:23
--  Android中級教程
大家好,我們這一節講的是手機頁面的轉換setContentView 的應用.在網頁的世界里,想要在兩個頁面間的轉換,只要利用超鏈接就可以實現,
但是在手機的世界里,要如何實現手機頁面的轉換呢? 最簡單的方法就是改變Activity 的Layout !
      在這個例子中,將布局兩個Layout ,分別為Layout1(main.xml) 和Layout2(mylayout.xml), 默認的Layout 為main.xml, 我們在Layout1 當中創建一個按鈕,當單擊按鈕時,顯示第二個Layout(mylayout.xml) ;同樣地,在Layout2 里也設計一個按鈕,當單擊第二個Layout 的按鈕之后,剛顯示回原來的Layout1 ,現在就來示范如何在兩個頁面之間互相切換.
首先看一下效果圖(為了區別兩個Layout ,我們分別設置了不同的背景色):
    1.jpg 2.jpg
下面是我們本程序所涉及的相關代碼,首先是主界面布局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();
            }
        });
    }
}
最后執行之!,這一節就到此結束~
主站蜘蛛池模板: 久久综合欧美成人| 18禁美女黄网站色大片免费观看 | 99久久精品免费观看国产| 又粗又大又爽又紧免费视频| 极品丝袜乱系列在线阅读| 9久久免费国产精品特黄| 伊人成影院九九| 天天舔天天射天天干| 爆乳女仆高潮在线观看| jzzjzz免费观看大片免费| 人体大胆做受大胆视频一| 夜夜夜夜猛噜噜噜噜噜试看| 正在播放国产精品| 3d动漫精品啪啪一区二区中| 亚洲国产日韩在线| 国产精品_国产精品_国产精品| 欧美中文字幕在线视频| 麻豆国产精品免费视频| 久久综合九色综合精品| 国产在线五月综合婷婷| 日产国产欧美视频一区精品| 精品成人AV一区二区三区| jizz视频在线观看| 亚洲精品无码不卡在线播放| 国产精品成人免费视频网站| 欧美性猛交xxx猛交| eeuss影院130020部| 亚洲日韩av无码中文| 国产女人在线观看| 快穿之性色无边(高h)| 男人扒开女人下面狂躁动漫版| 91精品国产福利在线观看| 亚欧洲精品在线视频免费观看 | 男人扒开女人下面狂躁动漫版 | 精品伊人久久大香线蕉网站| 99久久精品午夜一区二区| 亚洲av无码一区二区三区不卡| 国精产品一区一区三区MBA下载 | 好吊妞在线播放| 最近更新中文字幕第一页| 美女脱一净二净不带胸罩|