android页面切换

修改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"?>

<AbsoluteLayout

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="@color/black"

xmlns:android="http://schemas.android.com/apk/res/android">

<TextView

android: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>

<Button

android: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"?>

<AbsoluteLayout

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="@color/white"

xmlns:android="http://schemas.android.com/apk/res/android">

<TextView

android: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>

<Button

android: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()

            }

        } );

    }

}

你可能感兴趣的:(android)