实现安卓页面的跳转
2016-04-10 18:56
288 查看
在实际使用APP的时候,我们会发现按下某个按钮后,画面会进行切换,这样就可以把不同数据分门别类的放置在不同的页面上,一是避免画面过于拥挤,二是让用户的界面更为简洁,对程序的维护也有相当大的帮助。此功能的设计非常实用,我们在大多数的APP中都会看到这样的设计。
StartActivity -- 换页
(1)新建一个Android应用程序,打开资源文件目录中的 res\layout\main.xml ,增加一个新的Button按钮。
<Button android:text="Button"
android:id="@+id/button01" android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button></LinearLayout>
其中第12-17行:<Button>....</Button>加入一个Button按钮的UI元素。
第14行:android:id="@+id/button1"定义Button的控件句柄为Button1。
(2)新建第二个页面并打开 page2layout.xml 进行修改,这样就设计出第二个页面,XML文件内容如下
记得新建一个新的名为page2的Javaclass以便调用~
(3)修改源文件mainActivity.java
1,Button b1 = (Button) findViewById(R.id.button1);是取得控件句柄的函数,作用是取得main.xml中android:id取值为button1 的Button组件。
2,b1.setOnClickListener(new Button.OnClickListener).setAdapter(new ImageAdapter(this));设置按钮的控制都由this来处理,this的功能是指向本身的类。
3,为了触发Button行为,我们使用 onClick 函数。
4,Intent intent = new Intent();初始化一个Intent类。
5,intent.setClass(mainActivity.this,paga2.class);准备进入下一页这一动作,并且指定page2.class为下一页的类;
6,startActivity(intent);切换页面!(重点)
7,mainActivity.this.finish();关闭当前页面。
StartActivity -- 换页
(1)新建一个Android应用程序,打开资源文件目录中的 res\layout\main.xml ,增加一个新的Button按钮。
<span style="font-size:12px;"><strong></strong></span><pre name="code" class="html"><span style="font-size:12px;"><strong><?xml version="1.0" encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" <span style="white-space:pre"> </span>android:orientation="vertical" <span style="white-space:pre"> </span>android:layout_width="fill_parent" <span style="white-space:pre"> </span>android:layout_height="fill_parent" > <span style="white-space:pre"> </span><TextView <span style="white-space:pre"> </span>android:layout_width="fill_parent" <span style="white-space:pre"> </span>android:layout_height="wrap_content" <span style="white-space:pre"> </span>android:text="page2" <span style="white-space:pre"> </span>/> <span style="white-space:pre"> </span></strong></span>
<span style="font-size:12px;"><strong><span style="white-space:pre"> </span></LinearLayout></strong></span>
<Button android:text="Button"
android:id="@+id/button01" android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button></LinearLayout>
其中第12-17行:<Button>....</Button>加入一个Button按钮的UI元素。
第14行:android:id="@+id/button1"定义Button的控件句柄为Button1。
(2)新建第二个页面并打开 page2layout.xml 进行修改,这样就设计出第二个页面,XML文件内容如下
记得新建一个新的名为page2的Javaclass以便调用~
<span style="font-size:12px;"><strong><?xml version="1.0" encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" <span style="white-space:pre"> </span>android:orientation="vertical" <span style="white-space:pre"> </span>android:layout_width="fill_parent" <span style="white-space:pre"> </span>android:layout_height="fill_parent" > <span style="white-space:pre"> </span><TextView <span style="white-space:pre"> </span>android:layout_width="fill_parent" <span style="white-space:pre"> </span>android:layout_height="wrap_content" <span style="white-space:pre"> </span>android:text="@String/hello" <span style="white-space:pre"> </span>/> </LinearLayout></strong></span>
(3)修改源文件mainActivity.java
<span style="font-size:12px;"><strong>public class mainActivity extends Activity { public void onCreate(Bundle saveInstanceState){ <span style="white-space:pre"> </span>super.onCreate(R.layout.main); <span style="white-space:pre"> </span>Button b1 = (Button) findViewById(R.id.button1); <span style="white-space:pre"> </span>b1.setOnClickListener(new Button.OnClickListener() <span style="white-space:pre"> </span>public void onClick(View v){ <span style="white-space:pre"> </span>Intent intent = new Intent(); <span style="white-space:pre"> </span>intent.setClass(mainActivity.this,page2.class); <span style="white-space:pre"> </span>startActivity(instent); <span style="white-space:pre"> </span>mainActivity.this.finish(); <span style="white-space:pre"> </span>}); <span style="white-space:pre"> </span>} }</strong></span>这段代码中第15行:setContentView(R.layout.main);设定软件的显示界面为res\layout\main.xml。
1,Button b1 = (Button) findViewById(R.id.button1);是取得控件句柄的函数,作用是取得main.xml中android:id取值为button1 的Button组件。
2,b1.setOnClickListener(new Button.OnClickListener).setAdapter(new ImageAdapter(this));设置按钮的控制都由this来处理,this的功能是指向本身的类。
3,为了触发Button行为,我们使用 onClick 函数。
4,Intent intent = new Intent();初始化一个Intent类。
5,intent.setClass(mainActivity.this,paga2.class);准备进入下一页这一动作,并且指定page2.class为下一页的类;
6,startActivity(intent);切换页面!(重点)
7,mainActivity.this.finish();关闭当前页面。
相关文章推荐
- ubuntu搭建nodejs开发环境
- Hadoop2.6.0官方MapReduce文档翻译 之 二
- JSP内置对象out
- 【Oracle】3.模糊查询
- 线程池
- 使用ssh公钥密钥自动登陆linux服务器
- 算法--基础编程模型
- JVM核心机制_深入类加载器JAVA220-223
- 20145232 韩文浩 《Java程序设计》第6周学习总结
- 一元多项式乘法
- Hadoop2.6.0官方MapReduce文档翻译 之 一
- openstack 软重启和硬重启的区别
- 系统初始化脚本
- C++封装、继承、多态
- MOXA串口网关工作不正常解决方法
- 视觉中的深度学习方法CVPR 2012 Tutorial Deep Learning Methods for Vision
- Eclipse将引用了第三方jar包的Java项目打包成jar文件的两种方法
- 遍历目录方法
- HttpClient设置HTTP请求头Header
- 第六周学习进度情况