Intent实现Android间的页面跳转
16lz
2021-12-04
转帖请注明本文出自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- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="第二个Activity"/>
- LinearLayout>
Java代码
OtherActivity
[java] view plain copy- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- public class OtherActivity extends AppCompatActivity {
- @Override
- public void setContentView(View view) {
- super.setContentView(R.layout.activity_other);
- }
- }
程序主界面activity_main.xml
[html] view plain copy- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="第一个Activity"/>
- <Button
- android:id="@+id/start_btn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="页面跳转"/>
- LinearLayout>
Java代码
MainActivity
[java] view plain copy- import android.content.Intent;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- public class MainActivity extends AppCompatActivity {
- private Button startButton;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- startButton = findViewById(R.id.start_btn);
- startButton.setOnClickListener(new ButtonListener());
- }
- class ButtonListener implements View.OnClickListener{
- @Override
- public void onClick(View v) {
- //当点击事件触发后执行,启动OtherActivity
- //创建一个Intent对象
- Intent intent =new Intent();
- intent.setClass(MainActivity.this,OtherActivity.class);//从MainActivity跳转到OtherActivity
- startActivity(intent);
- }
- }
- }
另外除了上述的显式Intent,还有隐式Intent,隐式Intent可以用来传递数组及动作状态
比如在MainActivity中
[java] view plain copy- //当点击事件触发后执行,启动OtherActivity
- //创建一个Intent对象
- Intent intent =new Intent();
- intent.setClass(MainActivity.this,OtherActivity.class);//从MainActivity跳转到OtherActivity
- intent.putExtra("姓名","小李");
- startActivity(intent);
在被跳转的OtherActivity中
[java] view plain copy- Intent intent =getIntent();
- String name = intent.getStringExtra("姓名");
可以接收由MainActivity传来的数据
又或者
[java] view plain copy- Intent intent = new Intent(Intent.ACTION_DIAL);
- intent.setData(Uri.parse("tel:10086"));
- startActivity(intent);
可以调用拨打电话界面并设定预设号码为10086
还可以设置网址的跳转,显示地理位置等
如设置为跳转打开网址时,需要在AndroidManifast中注册一下
如下:
- <activity android:name=".MainActivity">
- <intent-filter>
- <action android:name="android.intent.action.VIEW"/>
- <category android:name="android.intent.category.DEFAULT"/>
- <data android:scheme="http"/>
- intent-filter>
- activity>
更多相关文章
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面
- android键盘弹出之后界面整体上移,顶出去了
- Android界面布局详解
- android键盘弹出之后界面整体上移,顶出去了
- android简单学习总结
- Android学习笔记-界面和数据存储以及一些零碎知识点
- 【Android(安卓)界面效果14】RelativeLayout里常用的位置属性
- Android用户界面 UI组件--AdapterView及其子类(二) AdapterViewA
- Android界面布局基本知识简述