Android中级教程之(四)----->页面的转换,利用setContentView方法
来源:互联网 发布:蓝光碟 软件 编辑:程序博客网 时间:2024/06/03 22:40
大家好,我们这一节讲的是手机页面的转换setContentView的应用.在网页的世界里,想要在两个页面间的转换,只要利用超链接就可以实现,
但是在手机的世界里,要如何实现手机页面的转换呢? 最简单的方法就是改变Activity的Layout!
在这个例子中,将布局两个Layout,分别为Layout1(main.xml)和Layout2(mylayout.xml),默认的Layout为main.xml,我们在Layout1当中创建一个按钮,当单击按钮时,显示第二个Layout(mylayout.xml);同样地,在Layout2里也设计一个按钮,当单击第二个Layout的按钮之后,刚显示回原来的Layout1,现在就来示范如何在两个页面之间互相切换.
首先看一下效果图(为了区别两个Layout,我们分别设置了不同的背景色):
下面是我们本程序所涉及的相关代码,首先是主界面布局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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="欢迎来到魏祝林的博客"
/>
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击进入Layout2"
/>
</LinearLayout>
其次我们在main.xml同一目录新建一个为mylayout.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"
android:background="#ffffffff"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Welcome to Mr Wei's blog"
/>
<Button
android:id="@+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击进入Layout1"
/>
</LinearLayout>
最后是我们的核心程序setContentViewDemo.java
package com.android.setContentViewDemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class setContentViewDemo extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 载入main.xml Layout
setContentView(R.layout.main);
// 以findViewById()取得Button对象并添加事件onClickLisener
Button bt1 = (Button) findViewById(R.id.bt1);
bt1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
goToLayout2();
}
});
}
// 将layout由main.xml切换成mylayout.xml
public void goToLayout2() {
// 将layout改成mylayout
setContentView(R.layout.mylayout);
Button b2 = (Button) findViewById(R.id.bt2);
b2.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
goToLayout1();
}
});
}
// 将layout由mylayout.xml切换成main.xml
public void goToLayout1() {
setContentView(R.layout.main);
Button bt1 = (Button) findViewById(R.id.bt1);
bt1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
goToLayout2();
}
});
}
}
最后执行之!,这一节就到此结束~
转自:http://blog.csdn.net/android_tutor/article/details/4811427
- Android中级教程之(四)----->页面的转换,利用setContentView方法
- Android中级教程(一)之----手机页面的转换setContentView的应用
- Android中级教程(一)之----手机页面的转换setContentView的应用
- Android中级教程(一)之----手机页面的转换setContentView的应用
- Android 页面间的转换(setContentView)
- Android学习笔记(八) —— 手机页面的转换 — setContentView的应用
- Android中级教程之(三)----->Intent的使用
- Android中级教程之--------进程的优先级
- 手机页面的转换----setContentView的应用
- 手机页面转换--关于setContentView的应用
- Android Activity的UI绘制流程之setContentView方法详解
- Android 中级教程之------Android MediaPlayer播放mp3的实例!
- Android中级教程之------Android MediaPlayer播放mp3的实例
- Android 中级教程之------Android MediaPlayer播放mp3的实例!
- Android 中级教程之------Android MediaPlayer播放mp3的实例!
- Android 中级教程之------Android MediaPlayer播放mp3的实例!
- Android中级教程之 Android应用程序的生命周期
- Android 中级教程之------Android MediaPlayer播放mp3的实例!
- IE、FF的基本注意事项
- POJ 1062 昂贵的聘礼 Dijkstra
- TCP连接/撤离中的三次握手与四次挥手过程
- 应对面试,准备的题目!!
- 使用dumpbin 查看lib 中函数名和参数
- Android中级教程之(四)----->页面的转换,利用setContentView方法
- 十二句话,十二分钟,理解一辈子
- 只为成功
- CSS浏览器兼容性问题详解总结
- spring 3.1 m 配置json
- 留个脚印
- ssh 安全的shell
- h.264 SODB RBSP EBSP的区别
- 服务器(IBM/DELL/HP)保修时间查询地址汇总