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

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


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


新建被跳转的页面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异步处理一:使用Thread+Handler实现非UI线程更新UI界面
  2. android键盘弹出之后界面整体上移,顶出去了
  3. Android界面布局详解
  4. android键盘弹出之后界面整体上移,顶出去了
  5. android简单学习总结
  6. Android学习笔记-界面和数据存储以及一些零碎知识点
  7. 【Android(安卓)界面效果14】RelativeLayout里常用的位置属性
  8. Android用户界面 UI组件--AdapterView及其子类(二) AdapterViewA
  9. Android界面布局基本知识简述

随机推荐

  1. Android(安卓)OpenGL(二) 学习《Android(
  2. Android 打开设置的各个页面
  3. Android(安卓)AutoLayout全新的适配方式
  4. android studio windows 报错 ninja: bui
  5. Android(安卓)任务栈
  6. Android工作总结之如何做一个优秀的Media
  7. Android 平台上玩DOS游戏
  8. android PreferenceActivity
  9. android 开发中的Log
  10. 极光推送(守护进程)