Android深入浅出系列之实例应用—手机页面之间的跳转
在网页里,我们可以通过超级链接从一个网页跳转到另外一个网页,在手机里面,要如何实现手机页面之间的跳转呢?
原理:通过布局文件和setContentView()方法配合来实现。通过点击第一个布局文件main.xml当中的按钮,加载第二个布局文件main2.xml,然后点击第二个布局文件main2.xml当中的按钮,加载第一个布局文件main.xml。
1.1:第一个布局文件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="@string/hello"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="跳到第二个手机页面"
android:id="@+id/btn1"
/>
</LinearLayout>
1.2:第二个布局文件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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello2"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="跳到第一个手机页面"
android:id="@+id/btn2"
/>
</LinearLayout>
1.3:字符文件stings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">我是第一个手机布局页面</string>
<string name="hello2">我是第二个手机布局页面</string>
<string name="app_name">setContentViewDemo</string>
</resources>
1.4:代码文件
package com.menglin.setcontentview;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity
{
private Button btn1 = null;
private Button btn2 = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//默认加载第一个布局文件
setContentView(R.layout.main);
//通过findViewById()方法得到第一个布局文件中的Button对象
btn1 = (Button) findViewById(R.id.btn1);
//给这个Button对象绑定监听器
btn1.setOnClickListener(new Button1Listener());
}
//第一个布局文件中的按钮的监听器
private class Button1Listener implements OnClickListener
{
@Override
public void onClick(View v)
{
//加载第二个布局文件
setContentView(R.layout.main2);
//通过findViewById()方法得到第二个布局文件中的Button对象
btn2 = (Button) findViewById(R.id.btn2);
//给这个Button对象绑定监听器
btn2.setOnClickListener(new Button2Listener());
}
}
//第二个布局文件中的按钮的监听器
private class Button2Listener implements OnClickListener
{
@Override
public void onClick(View v)
{
//加载第一个布局文件
setContentView(R.layout.main);
//通过findViewById()方法得到第一个布局文件中的Button对象
btn1 = (Button) findViewById(R.id.btn1);
//给这个Button对象绑定监听器
btn1.setOnClickListener(new Button1Listener());
}
}
}
注意:虽然是实现了界面的来回跳转,但是始终是同一个Activity,所以类变量,函数等都是公用的。
运行效果如下
当我们单击第一布局文件当中的按钮后,就会切换到第二个我们设计好的布局文件。
Android深入浅出系列之实例应用—手机页面之间的跳转相关推荐
- Android深入浅出系列之实例应用—简单的手指拖动图片,图片滑来滑去显示应用Gallery和BaseAdapter以及ImageView的使用...
前言 我们现在在随便一个手机上用手指在屏幕上滑来滑都可以去拖动图片,其实在Android里这很简单,下面我就给大家具体讲解一下. 思路 我们首先需要Gallery这个对象,俗称画廊对象,大家都知道 ...
- Android深入浅出系列之实例应用—弹出消息Toast对象的使用自定义方式(二)
阅读目录 一:前言 二:实例 三:运行效果 一:前言 Android中定义了一个Toast对象,用以弹出一个窗口来给予用户帮助和提示,和对话框不同的是,Toast并不是以独占方式显示的,它并不会抢夺用 ...
- Android深入浅出系列之实例应用—弹出消息Toast对象的使用纯文本方式(一)
阅读目录 一:Toast 二:实现步骤 一:Toast Toast是Android专属的提示信息的对象,它的使用非常的简单,但是用途却非常的广泛,Toast就是一个简短的信息,将要告诉用户的信息以一个 ...
- 页面之间的跳转方式和参数传递以及路由和生命周期
微信小程序拥有web网页和Application共同的特征,我们的页面都不是孤立存在的,而是通过和其他页面进行交互,来共同完成系统的功能.今天我们来研究小程序页面之间的跳转方式. 1.概述 在Andr ...
- 利用计算机浏览信息,利用计算机浏览信息时,可以实现任意页面之间的跳转,这种技术最恰当的说法是(??)...
利用计算机浏览信息时,可以实现任意页面之间的跳转,这种技术最恰当的说法是(??) 小儿推拿经穴大多集中于A:腰背部B:胸腹部C:头面部D:上肢部E:下肢部 做作屈伸练习前直后转时,髋关节及整个身体转向 ...
- java中程序跳转_java程序中先后台交互的两种实现方式以及页面之间的跳转
虽然如今市面上很流行先后端分离,可是在不少企业中仍是使用的是SSH框架,先后端不分离. 那么此类先后端有其自身独特的先后台交互的方式,可是也支持js/jquery.下面对这两种方式作一下概括.java ...
- 页面之间的跳转与交互
前言:想写这篇的原因,学到location对象以及之前大web大作业,想实现多个页面之间的跳转只能够通过a标签来实现,但是有些地方只用a标签显然是不够的,最终还是没有解决,全都是使用a标签--现在可以 ...
- 微信小程序实现两个页面之间的跳转
两个简单的页面 点击链接实现在两个页面之间的跳转 图片和文字自行设置 步骤 1:创建项目 2:删除多余无用文件和代码 最新版的微信开发者工具不支持创建空白项目,我们需要把原来多余的文件和代码删除 需要 ...
- React Native之通过createStackNavigator实现携带参数的页面与页面之间的跳转
1 实现的功能 在网上看React Native文档,我特码就想实现一个页面到另外一个页面的跳转,然后另外一个页面怎么获取参数,特么没找到一个说清楚的,要么太复杂,要么说了不理解,下面是我自己写的一 ...
最新文章
- 从Java类库看设计模式
- J2EE的13种核心技术
- PHP的mysqli扩展
- freemark静态页面中文乱码
- 小学计算机说课稿,小学信息技术说课稿《新建文件夹》
- [小程序]小程序框架的简单页面布局
- 两个很有用的进程间通信函数popen,pclose
- 收获,不止SQL优化——抓住SQL的本质--第八章
- 小程序对wxParse 使用
- Java旅游网站源码+页面
- PHP获取身份证年龄和性别
- 修复iPhone系统白苹果问题
- HDFS开启HA后,Hbase的REGIONSERVERS启动错误(Operation category READ is not supported in state standby)
- IDEA双击不报错打不开解决方法
- Win10离线安装.NET Framework 3.5的方法技巧(附离线安装包下载)
- cnn是深度神经网络吗,cnn神经网络算法
- 带你读AI论文丨SP21 Survivalism: Living-Off-The-Land 经典离地攻击
- 快钱CEO关国光:在家睡觉也可以赚钱
- FastStone Capture监视器上拍摄和拍摄
- 记录一下 KVM服务器的bbr加速