当前位置:操作系统 > 安卓/Android >>

多种方式实现Android页面布局的切换

 

多种方式实现页面切换

 

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

 

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

 

先上代码:

 

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,分别显示两个页面,分别有一个<TextView>和<Button>元素,具体代码如下:

 

Main.xml代码:

 

<?xml version="1.0"encoding="utf-8"?>

 

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

 

   android:layout_width="fill_parent"

 

   android:layout_height="fill_parent"

 

   android:orientation="vertical" >

 

 

 

   <TextView

 

       android:layout_width="fill_parent"

 

       android:layout_height="wrap_content"

 

       android:text="这是第一页"

 

     

 

       />

 

   <Button

 

       android:layout_width="fill_parent"

 

       android:layout_height="wrap_content"

 

       android:id="@+id/next"

 

       android:text="下一页"/>

 

  

 

</LinearLayout>

 

 

 

Two.xml代码:

 

<?xmlversion="1.0" encoding="utf-8"?>

 

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

 

   android:layout_width="fill_parent"

 

   android:layout_height="fill_parent"

 

    android:orientation="vertical">

 

 

 

     <TextView

 

       android:layout_width="fill_parent"

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,