转帖请注明本文出自weimeig的博客(https://blog.csdn.net/weimeig/article/details/79666786),请尊重他人的辛勤劳动成果,谢谢

应朋友们反馈的Android基础薄弱的问题,决定出一套Android基础教程,帮助大家复习,巩固Android基础,今天要讲的是Android中的Intent实现Android间的页面跳转。


增加Acrivity页面时,首先需要在MainActivity中对页面注册,比如

Intent实现Android间的页面跳转_第1张图片

新建被跳转的页面OtherActivity,其对应的xml文件如下

activity_other

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent">  
  5.     <TextView  
  6.         android:layout_width="wrap_content"  
  7.         android:layout_height="wrap_content"  
  8.         android:text="第二个Activity"/>  
  9. LinearLayout>  

Java代码

OtherActivity

[java]  view plain copy
  1. import android.support.v7.app.AppCompatActivity;  
  2. import android.view.View;  
  3.   
  4. public class OtherActivity extends AppCompatActivity {  
  5.     @Override  
  6.     public void setContentView(View view) {  
  7.         super.setContentView(R.layout.activity_other);  
  8.   
  9.     }  
  10. }  

程序主界面activity_main.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical">  
  6.   
  7.     <TextView  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="第一个Activity"/>  
  11.     <Button  
  12.         android:id="@+id/start_btn"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:text="页面跳转"/>  
  16. LinearLayout>  

Java代码

MainActivity

[java]  view plain copy
  1. import android.content.Intent;  
  2. import android.support.v7.app.AppCompatActivity;  
  3. import android.os.Bundle;  
  4. import android.view.View;  
  5. import android.widget.Button;  
  6.   
  7.   
  8. public class MainActivity extends AppCompatActivity  {  
  9.   
  10.     private Button startButton;  
  11.     @Override  
  12.     protected void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.activity_main);  
  15.         startButton = findViewById(R.id.start_btn);  
  16.         startButton.setOnClickListener(new ButtonListener());  
  17.     }  
  18.     class ButtonListener implements View.OnClickListener{  
  19.         @Override  
  20.         public void onClick(View v) {  
  21.             //当点击事件触发后执行,启动OtherActivity  
  22.             //创建一个Intent对象  
  23.             Intent intent =new Intent();  
  24.             intent.setClass(MainActivity.this,OtherActivity.class);//从MainActivity跳转到OtherActivity  
  25.             startActivity(intent);  
  26.         }  
  27.     }  
  28. }  

另外除了上述的显式Intent,还有隐式Intent,隐式Intent可以用来传递数组及动作状态

比如在MainActivity中

[java]  view plain copy
  1. //当点击事件触发后执行,启动OtherActivity  
  2. //创建一个Intent对象  
  3. Intent intent =new Intent();  
  4. intent.setClass(MainActivity.this,OtherActivity.class);//从MainActivity跳转到OtherActivity  
  5. intent.putExtra("姓名","小李");  
  6. startActivity(intent);  

在被跳转的OtherActivity中

[java]  view plain copy
  1. Intent intent =getIntent();  
  2. String name = intent.getStringExtra("姓名");  

可以接收由MainActivity传来的数据

又或者

[java]  view plain copy
  1. Intent intent = new Intent(Intent.ACTION_DIAL);    
  2.                 intent.setData(Uri.parse("tel:10086"));    
  3.                 startActivity(intent);    

可以调用拨打电话界面并设定预设号码为10086

还可以设置网址的跳转,显示地理位置等

如设置为跳转打开网址时,需要在AndroidManifast中注册一下 

如下:

[html]  view plain copy
  1. <activity  android:name=".MainActivity">    
  2.             <intent-filter>    
  3.                 <action android:name="android.intent.action.VIEW"/>    
  4.                 <category android:name="android.intent.category.DEFAULT"/>    
  5.                 <data android:scheme="http"/>    
  6.             intent-filter>    
  7.                 
  8.         activity>    


个人分类:  Android

更多相关文章

  1. Android界面布局详解
  2. 【Android 界面效果14】RelativeLayout里常用的位置属性
  3. Android学习笔记-界面和数据存储以及一些零碎知识点
  4. Android界面布局基本知识简述
  5. 1.4 android——UI之 UI界面属性用法与注意点汇总

随机推荐

  1. Android定制视图及手势检测的基本示例
  2. Android(安卓)MVC 架构详解
  3. android button 背景样式
  4. Android中线程与进程的理解
  5. 【Android企业级开发案例分享】仿西瓜视
  6. Android适配不同的dpi和屏幕尺寸
  7. Android(安卓)NDK 之NEON优化
  8. 动手撸一个Android路由框架LuRouter
  9. 【android基础】Android中四大组件
  10. Android(安卓)自定义View系列之必备api