android頁面切換

來源:互聯網
上載者:User

修改strings.xml

<?xml version="1.0" encoding="utf-8"?><resources><string name="layout1">this is Layout 1</string><string name="layout2">This is Layout 2</string><string name="app_name">Ex8_UI</string></resources>

建立color.xml 儲存兩個顏色值 (放到res/values目錄中)

<?xml version="1.0" encoding="utf-8"?><resources><color name="black">#000000</color><color name="white">#FFFFFFFF</color></resources>

修改main.xml 布局,添加一個TextView 和一個Button

<?xml version="1.0" encoding="utf-8"?><AbsoluteLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:background="@color/black"xmlns:android="http://schemas.android.com/apk/res/android"><TextViewandroid:id="@+id/text1"android:textSize="24sp"android:layout_width="186px"android:layout_height="29px"android:layout_x="70px"android:layout_y="32px"android:text="@string/layout1"></TextView><Buttonandroid:id="@+id/button1"android:layout_width="118px"android:layout_height="wrap_content"android:layout_x="100px"android:layout_y="82px"android:text="Go to Layout2"></Button></AbsoluteLayout>

建立mylayout.xml 布局檔案(放到res/layout目錄中),並添加兩個View:TextView 和Button

<?xml version="1.0" encoding="utf-8"?><AbsoluteLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:background="@color/white"xmlns:android="http://schemas.android.com/apk/res/android"><TextViewandroid:id="@+id/text2"android:textSize="24sp"android:layout_width="186px"android:layout_height="29px"android:layout_x="70px"android:layout_y="32px"android:textColor="@color/black"android:text="@string/layout2"> </TextView><Buttonandroid:id="@+id/button2"android:layout_width="118px"android:layout_height="wrap_content"android:layout_x="100px"android:layout_y="82px"android:text="Go to Layout1"></Button></AbsoluteLayout>

編寫mainActivity.java

/** To change this template, choose Tools | Templates* and open the template in the editor.*/package org.me.setcontentviewapp;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Button;/**** @author LiaoKeCheng<http://hi.baidu.com/wishwingliao>*/public class MainActivity extends Activity{    /** Called when the activity is first created. */    @Override    public void onCreate( Bundle icicle )    {        super.onCreate( icicle );        /* 載入main.xml Layout */        setContentView( R.layout.main );// 預設啟動布局        /* 以findViewById()取得Button 對象,並添加onClickListener */        Button b1 = ( Button ) findViewById( R.id.button1 );        b1.setOnClickListener( new Button.OnClickListener()        {            public void onClick( View v )            {                jumpToLayout2();// 調用跳轉方法jumpToLayout2()            }        } );    }    /**     * method jumpToLayout2:將layout由 main.xml 切 換成mylayout.xml     */    public void jumpToLayout2()    {        /* 將layout 改成mylayout.xml */        setContentView( R.layout.mylayout );        /* 以findViewById()取得Button 對象,並添加onClickListener */        Button b2 = ( Button ) findViewById( R.id.button2 );        b2.setOnClickListener( new Button.OnClickListener()        {            public void onClick( View v )            {                jumpToLayout1();// 調用跳轉方法jumpToLayout1()            }        } );    }    /**     * method jumpToLayout1:將layout 由mylayout.xml 切換成main.xml     */    public void jumpToLayout1()    {        /* 將layout 改成main.xml */        setContentView( R.layout.main );        /* 以findViewById()取得Button 對象,並添加onClickListener */        Button b1 = ( Button ) findViewById( R.id.button1 );        b1.setOnClickListener( new Button.OnClickListener()        {            public void onClick( View v )            {                jumpToLayout2();// 調用跳轉方法jumpToLayout2()            }        } );    }}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.