1.页面一main.xml代码如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/text1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/layout1"/> <Button android:id="@+id/btn1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Go to Layout2" /> </LinearLayout>
页面二mylayout.xml代码如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/text2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/layout2"/> <Button android:id="@+id/btn2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Go to Layout1" /> </LinearLayout>
2.java程序如下:
package org.lxh.demo; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnFocusChangeListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class Hello extends Activity { private Button btn1 = null; // private Button btn2=null; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 生命周期方法 super.setContentView(R.layout.main); // 设置要使用的布局管理器 this.btn1 = (Button) super.findViewById(R.id.btn1); btn1.setOnClickListener(new Button.OnClickListener() { public void onClick(View arg0) { jumpToLayout2(); } }); } public void jumpToLayout2() { setContentView(R.layout.mylayout); Button btn2 = (Button) super.findViewById(R.id.btn2); btn2.setOnClickListener(new Button.OnClickListener() { public void onClick(View arg0) { jumpToLayout1(); } }); } public void jumpToLayout1() { setContentView(R.layout.main); Button btn1 = (Button) super.findViewById(R.id.btn1); btn1.setOnClickListener(new Button.OnClickListener() { public void onClick(View arg0) { jumpToLayout2(); } }); } }
3.运行如下:
时间: 2024-10-23 05:31:12