使用setContentView可以在Activity中动态切换显示的View,这样,不需要多个Activity就可以显示不同的界面,因此不再需要在Activity间传送数据,变量可以直接引用。但是,在android SDK给我们建的默认的Hello World程序中,调用的是setContentView(int layoutResID)方法,如果使用该方法切换view,在切换后再切换回,无法显示切换前修改后的样子,也就是说,相当于重新显示一个view,并非是把原来的view隐藏后再显示。其实setContentView是个多态方法,我们可以先用LayoutInflater把布局xml文件引入成View对象,再通过setContentView(View view)方法来切换视图。因为所有对View的修改都保存在View对象里,所以,当切换回原来的view时,就可以直接显示原来修改后的样子。
下面是代码,main.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView android:id="@+id/tv1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="main.xml"
- />
- <Button android:id="@+id/b1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="切换到main2.xml"
- />
- <Button android:id="@+id/b3"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="修改tv1"
- />
- </LinearLayout>
main2.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView android:id="@+id/tv2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="main2.xml"
- />
- <Button android:id="@+id/b2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="切换到main.xml"
- />
- </LinearLayout>
Java 代码:
- package com.pocketdigi.changeView;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- public class Main extends Activity {
- /** Called when the activity is first created. */
- TextView tv1, tv2;
- Button b1, b2,b3;
- View layout1, layout2;
- boolean view2Load=false;//main2是否载入过的flag
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- //LayoutInflater inflater = getLayoutInflater();
- LayoutInflater inflater = LayoutInflater.from(this);
- //以上两行功能一样
- layout1 = inflater.inflate(R.layout.main, null);
- layout2 = inflater.inflate(R.layout.main2, null);
- setView1();
- tv1 = (TextView) findViewById(R.id.tv1);
- b1 = (Button) findViewById(R.id.b1);
- b3 = (Button) findViewById(R.id.b3);
- b3.setOnClickListener(l3);
- b1.setOnClickListener(l1);
- //控件及监听器只需一次查找绑定,切换view不影响
- }
- private void setView1() {
- setContentView(layout1);
- //切换到main
- }
- private void setView2() {
- setContentView(layout2);
- //切换到main2
- find();
- }
- private void find(){
- if(!view2Load){//如果首次显示main2,查找main2里的控件并绑定监听器
- tv2 = (TextView) findViewById(R.id.tv2);
- b2 = (Button) findViewById(R.id.b2);
- b2.setOnClickListener(l2);
- view2Load=true;//flag设为true
- }
- }
- OnClickListener l1 = new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- setView2();
- }
- };
- OnClickListener l2 = new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- setView1();
- }
- };
- OnClickListener l3 = new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- tv1.setText("已修改");
- //修改tv2的值,切换到main2后再切换回来,可以发现tv1的值被保存了,还是"已修改"
- }
- };