欧美日韩一区二区高清,久久视频这里只有精品,超黄网站在线观看,成人av影院在线观看


曙海教育集團(tuán)論壇3G手機(jī)技術(shù)專區(qū)Android應(yīng)用開發(fā) → Android中級(jí)教程


  共有6235人關(guān)注過本帖樹形打印

主題:Android中級(jí)教程

美女呀,離線,留言給我吧!
wangxinxin
  1樓 個(gè)性首頁(yè) | 博客 | 信息 | 搜索 | 郵箱 | 主頁(yè) | UC


加好友 發(fā)短信
等級(jí):青蜂俠 帖子:1393 積分:14038 威望:0 精華:0 注冊(cè):2010-11-12 11:08:23
Android中級(jí)教程  發(fā)帖心情 Post By:2010-12-3 11:06:23

大家好,我們這一節(jié)講的是手機(jī)頁(yè)面的轉(zhuǎn)換setContentView 的應(yīng)用.在網(wǎng)頁(yè)的世界里,想要在兩個(gè)頁(yè)面間的轉(zhuǎn)換,只要利用超鏈接就可以實(shí)現(xiàn),
但是在手機(jī)的世界里,要如何實(shí)現(xiàn)手機(jī)頁(yè)面的轉(zhuǎn)換呢? 最簡(jiǎn)單的方法就是改變Activity 的Layout !
      在這個(gè)例子中,將布局兩個(gè)Layout ,分別為L(zhǎng)ayout1(main.xml) 和Layout2(mylayout.xml), 默認(rèn)的Layout 為main.xml, 我們?cè)贚ayout1 當(dāng)中創(chuàng)建一個(gè)按鈕,當(dāng)單擊按鈕時(shí),顯示第二個(gè)Layout(mylayout.xml) ;同樣地,在Layout2 里也設(shè)計(jì)一個(gè)按鈕,當(dāng)單擊第二個(gè)Layout 的按鈕之后,剛顯示回原來(lái)的Layout1 ,現(xiàn)在就來(lái)示范如何在兩個(gè)頁(yè)面之間互相切換.
首先看一下效果圖(為了區(qū)別兩個(gè)Layout ,我們分別設(shè)置了不同的背景色):
    1.jpg 2.jpg
下面是我們本程序所涉及的相關(guān)代碼,首先是主界面布局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="歡迎來(lái)到魏祝林的博客"
    />
<Button
    android:id="@+id/bt1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="點(diǎn)擊進(jìn)入Layout2"
/>
</LinearLayout>
其次我們?cè)趍ain.xml 同一目錄新建一個(gè)為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="點(diǎn)擊進(jìn)入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對(duì)象并添加事件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();
            }
        });
    }
}
最后執(zhí)行之!,這一節(jié)就到此結(jié)束~

支持(0中立(0反對(duì)(0單帖管理 | 引用 | 回復(fù) 回到頂部

返回版面帖子列表

Android中級(jí)教程








簽名
主站蜘蛛池模板: 黄梅县| 齐河县| 汶上县| 宾川县| 昂仁县| 河西区| 盐亭县| 灵台县| 务川| 郯城县| 鄯善县| 中超| 宁海县| 日喀则市| 清水河县| 乃东县| 邵阳县| 永安市| 临桂县| 丽江市| 弥渡县| 雅江县| 雷山县| 灵丘县| 衡山县| 上饶县| 云阳县| 沙田区| 芦溪县| 临清市| 东明县| 荔浦县| 潍坊市| 资阳市| 沐川县| 监利县| 延津县| 松阳县| 宾川县| 论坛| 阿图什市|