Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3426489
  • 博文数量: 864
  • 博客积分: 14125
  • 博客等级: 上将
  • 技术积分: 10634
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-27 16:53
个人简介

https://github.com/zytc2009/BigTeam_learning

文章分类

全部博文(864)

文章存档

2023年(1)

2021年(1)

2019年(3)

2018年(1)

2017年(10)

2015年(3)

2014年(8)

2013年(3)

2012年(69)

2011年(103)

2010年(357)

2009年(283)

2008年(22)

分类: Java

2010-11-15 17:01:21

1.使用setContentView

    最简单的方法就是改变ActivityLayout例如:有两个Layout ,分别为Layout1(main.xml)Layout2(layout.xml), 默认的Layoutmain.xml, 我们在Layout1 当中创建一个按钮,当单击按钮时,显示第二个Layout(layout.xml) ;同样地,在Layout2 里也设计一个按钮,当单击第二个Layout 的按钮之后,刚显示回原来的Layout1 ,现在就来示范如何在两个页面之间互相切换.在这个例子中,将布局两个Layout ,分别为Layout1(main.xml)Layout2(mylayout.xml), 默认的Layoutmain.xml, 我们在Layout1 当中创建一个按钮,当单击按钮时,显示第二个Layout(mylayout.xml) ;同样地,在Layout2 里也设计一个按钮,当单击第二个Layout 的按钮之后,刚显示回原来的Layout1 ,现在就来示范如何在两个页面之间互相切换.

首先看一下效果图(为了区别两个Layout ,我们分别设置了不同的背景色):

 

下面是我们本程序所涉及的相关代码,首先是主界面布局main.xml


    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="欢迎来到魏祝林的博客"
    />
    android:id="@+id/bt1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="点击进入Layout2"
/>

其次我们在main.xml 同一目录新建一个为mylayout.xml 文件,代码如下:


    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffffff"
    >
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Welcome to Mr Wei's blog"
    />
    android:id="@+id/bt2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="点击进入Layout1"
/>

最后是我们的核心程序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();
            }
        });
    }

}

2.创建新的Activity

    但是这需要考虑是否结束原来的Activity如果还需要返回原来的界面可以不处理,否则需要finish原来的Activity

 前一个教程介绍了如何运用切换Layout 的方式进行手机页面间的转换,如果要转换的页面不只是背景,颜色或文字内容的不同,而是Activity 的置换,那,那就不是单单改变Layout 就能完成的,尤其是需要传递的变量不像网页可以通过CookieSession ,在程序里要移交主动权到另外一个Activity ,光靠先前技巧是办不到的.

而下面我们要讲的Intent 对象就是为解决这问题而生的,Intent 就如同其英文字义,是"想要"或"意图",之意,在主Activity 当中,告诉程序自己是什么,并想要前往哪里,这就是Intent 对象所处理的事了,本例子和前一个例子我们将实现同一个效果.

看一下效果图:

 

下面是所涉及的代码:

首先是布局main.xmlmylayout.xml

main.xml:


    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="欢迎来到魏祝林的博客"
    />
    android:id="@+id/bt1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="点击进入Layout2"
/>

mylayout.xml


    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffffff"
    >
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Welcome to Mr Wei's blog"
    />
    android:id="@+id/bt2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="点击进入Layout1"
/>

然后是控制程序IntentDemo.javaIntentDemo1.java 代码:

IntentDemo.java:

package com.android.test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


public class IntentDemo extends Activity {
 
    private Button bt1;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        bt1 = (Button)findViewById(R.id.bt1);
       
        bt1.setOnClickListener(new Button.OnClickListener(){
            public void onClick(View v){
                //new 一个Intent对象,并指定要启动的Class
                Intent intent = new Intent();             
                intent.setClass(IntentDemo.this, IntentDemo1.class);         
                //调用一个新的Activity
                startActivity(intent);
                //关闭原本的Activity
                IntentDemo.this.finish();

            }
        });
    }
}

IntentDemo.java 同一目录内新建一个IntentDemo1.java

IntentDemo1.java:

package com.android.test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class IntentDemo1 extends Activity {

    private Button bt2;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 载入mylayout.xml
        setContentView(R.layout.mylayout);

        bt2 = (Button) findViewById(R.id.bt2);
        bt2.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                // new 一个Intent对象,并指定要启动的Class
                Intent intent = new Intent();
                intent.setClass(IntentDemo1.this, IntentDemo.class);
                // 调用一个新的Activity
                startActivity(intent);
                // 关闭原本的Activity
                IntentDemo1.this.finish();

            }
        });
    }
}

最后是本例子的重点,添加另外一个Activity 所以必须在AndroidManifest.xml 中定义一个新的activty ,并给予名称name ,或则程序无法编译运行.新手很容易遇到这个问题.


      package="com.android.test"
      android:versionCode="1"
      android:versionName="1.0">
   
                          android:label="@string/app_name">
           
               
               
           

       

       
   

   

本例子所涉及的的全部代码已经全部贴出,最后执行之,将达到上述效果


阅读(653) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~