Android页面跳转的方法
来源:互联网 发布:lca在线算法 编辑:程序博客网 时间:2024/05/18 02:10
Android页面跳转的方法
方法一:Intent进行跳转:
Intent方法进行页面跳转适用于两个Activity之间的跳转,按返回键可以直接返回到前一个页面。
使用时需要注意在Manifest.xml文件中注册Activity!
//Intent类有多个构造方法Intent intent = new Intent();intent.setAction(action);//等同于使用actionintent.setClass(packageContext, cls);//等同于使用类名Intent intent = new Intent(action);Intent intent = new Intent(packageContext, cls);
第二个构造方法很简单,第一个参数为本Activity的上下文环境,第二个参数为跳转到的Activity类,如下:
//第一个参数看情况而定,有时候需要加上本Activity的类名FirstActivity.this
Intent intent = new Intent(this,SecondActivity.class);startActivity(intent);
我想演示一下使用action的方式,顺便看一下action怎么用:
使用action进行跳转,Activity在Manifest.xml中注册时要有一个IntentFilter段,只有在这个段中包含了相同action名,Activity才能与这个Intent匹配进行跳转。
Action的值在Android中有很多预定义,如果需要直接转到自定义的Intent接收者中,则需要在IntentFilter中加入自定义的Action值(同时需要设定Category值为“android.intent.category.DEFAULT”,→_←,我并不知道为什么,反正不加会报错的!),并在Intent中设定响应的Action,这样就能进行匹配,因为这个Action在系统中是唯一的。
明确如下定义:(抄来的)
1.ACTION的值是一个类型为String的字符串
2.Intent中ACTION的值是双向的,发送方发给接受方中的ACTION值能够被双方了解!这样在发送方中,自定义ACTION的值,在接受方中,Intent-filter中的ACTION值要与自定义的值相同,这样才能够正确接受Intent
3.自定义的ACTION值最好格式为:这个字符串最好是package name + activity class name+your action
示例如下:
//FirstActivity.javapackage com.example.pagejump;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;public class FirstActivity extends Activity { public static final String MYACTION = "com.example.pagejump.FirstActivity.JumpToSecondActivity"; private Button firstPageButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_first); initView(); } private void initView() { firstPageButton = (Button) findViewById(R.id.firstBtn); } public void firstBtnOnClick(View view){ Intent intent = new Intent(); intent.setAction(MYACTION); startActivity(intent); }}
//SecondActivity.javapackage com.example.pagejump;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;public class SecondActivity extends Activity { public static final String MYACTION2 = "com.example.pagejump.FirstActivity.JumpToFirstActivity"; private Button secondPageButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); initView(); } private void initView() { secondPageButton = (Button) findViewById(R.id.secondBtn); } public void secondBtnOnClick(View view){ Intent intent = new Intent(); startActivity(intent); }}
清单文件如下:
<activity android:name=".FirstActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <action android:name="com.example.pagejump.FirstActivity.JumpToFirstActivity"/> <category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </activity> <activity android:name=".SecondActivity"> <intent-filter> <action android:name="com.example.pagejump.FirstActivity.JumpToSecondActivity"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </activity>
两个activity的布局文件代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="${relativePackage}.${activityClass}" > <Button android:id="@+id/firstBtn" android:layout_marginTop="40dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点击此处跳转到第二个页面" android:onClick="firstBtnOnClick"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这里是第一个页面" /></LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000000" android:orientation="vertical" tools:context="${relativePackage}.${activityClass}" > <Button android:id="@+id/secondBtn" android:layout_marginTop="40dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点击此处跳转到第一个页面" android:background="#7D5D94" android:onClick="secondBtnOnClick"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这里是第二个页面" android:textColor="#FF0000"/></LinearLayout>
方法二:使用布局文件以及setContentView()方法实现
适合同一Activity里的不同View之间跳转,虽然是实现了界面的来回跳转,但是始终是同一个Activity,所以类变量,函数等都是公用的!按返回键也不会返回到前一页面,需要自己添加按键监听代码来实现,
两个布局文件如上所示,接下来只要在同一个Activity类中写上两个页面的点击button进行转换的代码:
package com.example.pagejump;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;public class FirstActivity extends Activity { //public static final String MYACTION = // "com.example.pagejump.FirstActivity.JumpToSecondActivity"; private Button firstPageButton; private Button secondPageButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //设置第一个布局文件为默认的加载界面 setContentView(R.layout.activity_first); initView(); } //获取两个Button对象 private void initView() { firstPageButton = (Button) findViewById(R.id.firstBtn); secondPageButton = (Button) findViewById(R.id.secondBtn); } //添加第一个Button对象的监听器 public void firstBtnOnClick(View view){ //加载第二个布局文件 setContentView(R.layout.activity_second); } //添加第二个Button对象的监听器 public void secondBtnOnClick(View view){ //加载第一个布局文件 setContentView(R.layout.activity_first); }}
- Android页面跳转的方法
- 页面跳转的方法
- 页面跳转的方法
- 页面跳转的方法
- Android自动跳转页面和自杀页面的方法
- android 的页面跳转
- Android的页面跳转
- Android的页面跳转
- android自学 intent 页面跳转的两种方法
- Android启动页面定时跳转的三种方法
- Android启动页面定时跳转的三种方法
- Android启动页面定时跳转的三种方法
- Javascript跳转页面的方法
- 页面跳转的各种方法
- php页面跳转的方法
- flex 页面跳转的方法
- 页面跳转的方法收集
- servlet跳转页面的方法
- JVM之——垃圾回收简介
- iOS图片加水印效果的实现并保存至相册
- (WIP)DPDK support for vhost-user学习笔记(by quqi99)
- maven项目中加载oracle驱动
- [IOS]今天开始学UI---UIPickerView
- Android页面跳转的方法
- linux oracle10g命令行创建数据库实例
- My first CSDN
- noip1996 挖地雷 - 提高组 (搜索)
- Java解决Zip文件的解压
- PC端网站自动识别为移动版的前端代码!
- 移动前端手机输入法自带emoji表情字符处理
- 【web基础1】你是如何打开百度的?
- warning LNK4006: __NULL_IMPORT_DESCRIPTOR 已在 shlwapi.lib(SHLWAPI.dll) 中定义;已忽略第二个定义