---
title: "多种方式实现Android页面布局的切换"
author: "mmdev"
date: "2011-12-08T18:07:00Z"
tags: []
slug: "android"
draft: false
lastmod: "2011-12-10T16:32:07Z"
id: 1301746
url: "https://mmdev.iteye.com/blog/1301746"
---

多种方式实现页面切换

今天老师留的作业如题，要求用三种方式实现：按钮切换，按键切换和触摸切换。

先说我做的第一种方式逻辑：

先上代码：

OneActivity.java文件代码：

package cn.class3g;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class OneActivity extends Activity {

/\** Called when the activity is first created. \*/

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

Button nextButton = (Button)findViewById(R.id.next);

nextButton.setOnClickListener(new OnClickListener() {

@Override

publicvoid onClick(View v) {

setContentView(R.layout.two);

ButtonupButton=(Button)findViewById(R.id.up);

upButton.setOnClickListener(newOnClickListener() {

@Override

publicvoid onClick(View v) {

setContentView(R.layout.main);

ButtonnextButton = (Button)findViewById(R.id.next);

}

});

}

});

}

}

解释：这是我最初写的代码，布局文件写了两个：main.xml和two.xml，分别显示两个页面，分别有一个&lt;TextView&gt;和&lt;Button&gt;元素，具体代码如下：

Main.xml代码：

&lt;?xml version="1.0"encoding="utf-8"?&gt;

&lt;LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

android:layout\_width="fill\_parent"

android:layout\_height="fill\_parent"

android:orientation="vertical" &gt;

&lt;TextView

android:layout\_width="fill\_parent"

android:layout\_height="wrap\_content"

android:text="这是第一页"

/&gt;

&lt;Button

android:layout\_width="fill\_parent"

android:layout\_height="wrap\_content"

android:id="@+id/next"

android:text="下一页"/&gt;

&lt;/LinearLayout&gt;

Two.xml代码：

&lt;?xmlversion="1.0" encoding="utf-8"?&gt;

&lt;LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

android:layout\_width="fill\_parent"

android:layout\_height="fill\_parent"

android:orientation="vertical"&gt;

&lt;TextView

android:layout\_width="fill\_parent"

android:layout\_height="wrap\_content"

android:text="这是第二页"

/&gt;

&lt;Button

android:layout\_width="fill\_parent"

android:layout\_height="wrap\_content"

android:id="@+id/up"

android:text="上一页"

/&gt;

&lt;/LinearLayout&gt;

两个xml文件写完后就是写逻辑了，java代码如开头写的那样，我是这样想的：

默认的onCreate方法初始化后，通过setContentView调用布局文件main。Xml这就是默认的布局，然后声明了一个Button组件命名为nextButton，调用setOnClickListener监听器实现对按钮的监听，在监听器中new了一个点击监听器onclickListener，在监听器中实现了一个名为onClick的方法，在这里我写了一个跳转页面的setContentView方法，通过点击Button按钮实现跳转到two.xml布局页面上，这样就打开了two.xml页面；在这个页面上写了另一个Button按钮命名为：upButton用来返回上页面，就这样通过嵌套就实现了通过点击按钮实现页面切换的效果。

但这不是我想要的效果，我要的效果是无论点击哪个按钮都会出现不同的样式页面，所以我通过对按钮进行判断来实现效果。首先想到的是用if语句进行判断点击的是哪个按钮，然后进行不同的操作。我想这样好点吧，但事实上不需要那么复杂去判断键值就可以实现：

packagecn.class3g;

importandroid.app.Activity;

importandroid.os.Bundle;

importandroid.view.View;

importandroid.widget.Button;

public classActivityTest extends Activity {

/\** Called when the activity is firstcreated. \*/

@Override

public void onCreate(BundlesavedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

Button nextButton =(Button)findViewById(R.id.next);

nextLayout();//显示下一个页面

}

public void nextLayout(){

setContentView(R.layout.two);

ButtonupButton=(Button)findViewById(R.id.up);

upButton.setOnClickListener(newView.OnClickListener() {

@Override

public voidonClick(View v) {

upLayout();

}

});

}

public void upLayout(){

setContentView(R.layout.main);

ButtonnextButton = (Button)findViewById(R.id.next);

nextButton.setOnClickListener(newView.OnClickListener() {

@Override

public voidonClick(View v) {

nextLayout();//显示下一个桌面

}

});

}

}

然后是通过按键实现页面切换，通过老师上午的实例可以知道，在调用onClickDown或onClickUp的方法中调用页面布局文件就可以实现：setContentView(R.layout.main);，在OnClickDown和onClickUp中分别添加两个页面的布局文件，再添加相反的方法，实现返回即可，代码如下：

public boolean onKeyDown(int keyCode,KeyEvent event) {

this.setContentView(R.layout.main);

returnsuper.onKeyDown(keyCode, event);

}

public boolean onKeyUp(int keyCode,KeyEvent event){

//showInfo("keyUp"+keyCode);

this.setContentView(R.layout.second);

return super.onKeyUp(keyCode,event);

}

触摸实现换页

public booleanonTouchEvent(MotionEvent event){

//showInfo("ontouch：x=" +event.getX() + " y="+event.getY());

flag = !flag;

if(flag){

this.setContentView(R.layout.main);

}else{

this.setContentView(R.layout.second);

returnsuper.onTouchEvent(event);

}

总结：

调用布局文件：setContentView(R.layout.main);

获取组件ID：ButtonnextButton = (Button)findViewById(R.id.next);

做题的过程中重命名了一次java文件，导致无法调试，原因是：忘记更改AndroidManifest.xml文件中的&lt;activity&gt;下的&lt;android:name&gt;属性值。