android 通过setContentView切换Activity的View,保存动态修改后的视图
2014-09-22 11:58
302 查看
使用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:
main2.xml:
Java 代码:
view)方法来切换视图。因为所有对View的修改都保存在View对象里,所以,当切换回原来的view时,就可以直接显示原来修改后的样子。
下面是代码,main.xml:
<span style="font-size:14px;"><?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></span>
main2.xml:
<span style="font-size:14px;"><?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></span>
Java 代码:
<span style="font-size:14px;">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的值被保存了,还是"已修改" } }; }</span>
相关文章推荐
- Android-- 通过setContentView切换Activity的View,保存动态修改后的视图
- Android-- 通过setContentView切换Activity的View,保存动态修改后的视图
- android 通过setContentView切换Activity的View,保存动态修改后的视图
- Android学习笔记03: 通过setContentView切换Activity的View,保存动态修改后的视图
- Android中通过Tag为View保存数据绑定数据
- Android 通过Java代码生成创建界面。动态生成View,动态设置View属性。addRules详解
- Android编程动态创建视图View的方法
- Android 通过Java代码生成创建界面。动态生成View,动态设置View属性。addRules详解
- android 通过Camera和Surfaceview实现照相 图片预览 保存图片到本地 手动聚焦 FaceDetectionListener实现人脸识别
- Android 通过Java代码生成创建界面。动态生成View,动态设置View属性。addRules详解
- 动态调整Android surfaceView 视图上下层问题
- Android中图片处理(压缩 保存 显示 截屏 视图View转为图片Bitmap)函数工具类
- android一个动态添加删除网格视图的demo,可把状态保存在数据库中
- Android\OPhone自定义视图(View) 推荐
- 让人糊涂的TrackViewState()与视图状态保存
- android用户界面-组件Widget-网络视图WebView
- 动态调整android控件(View)的大小
- android ListView布局之三(使用自定义的Adapter绑定数据,通过contextView.setTag绑定数据)有按钮的ListView
- Android通过onDraw实现在View中绘图操作
- android中使用selector动态改变imageView或ImageViewButton的背景