Android使用Intent实现页面跳转

所属分类: 软件编程 / Android 阅读数: 46
收藏 0 赞 0 分享

什么是Intent

Intent可以理解为信使(意图)
由Intent来协助完成Android各个组件之间的通讯

Intent实现页面之间的跳转

1>startActivity(intent)
2>startActivityForResult(intent,requestCode)
onActivityResult(int requestCode,int resultCode,Intent data)
setResult(resultCode,data)
第二种启动方式可以返回结果

代码

FirstActivity.java

public class FirstActivity extends AppCompatActivity {
  private Button bt1;
  private Button bt2;
  private TextView tv;
  private Context context=FirstActivity.this;

  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.first_layout);

    bt1 = (Button) findViewById(R.id.button_first);
    bt2 = (Button) findViewById(R.id.button_second);
    tv = (TextView) findViewById(R.id.textView);
    /*
    * 通过点击bt1实现页面之间的跳转
    * 1.startActivity的方式来实现
    * */
    bt1.setOnClickListener(new View.OnClickListener() {
      public void onClick(View view) {
        /*
        * 第一个参数:上下文对象.this 匿名内部类中不能直接.this
        * 第二个参数:目标文件(注意是.class)
        * Intent(Context packageContext, Class<?> cls)
        * */
        //Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
        Intent intent = new Intent(context,SecondActivity.class);
        startActivity(intent);
      }
    });

    /*
    * 通过startActivityForResult
    * */
    bt2.setOnClickListener(new View.OnClickListener() {
      public void onClick(View view) {
        Intent intent = new Intent(context,SecondActivity.class);
        /*
         * 第一个参数是Intent对象
         * 第二个参数是请求的一个标识
         * public void startActivityForResult(Intent intent, int requestCode)
         */
        startActivityForResult(intent,1);
      }
    });
  }
  /*
   * 通过startActivityForResult方法跳转,接收返回数据的方法
   * requestCode:请求的标识
   * resultCode:第二个页面返回的标志
   * data:第二个页面回传的数据
   */
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == 1 && resultCode == 3){
      String value = data.getStringExtra("data");
      tv.setText(value);
    }
  }
}

SecondActivity.java

public class SecondActivity extends Activity {
  private Button bt1;
  private String value="回传数据";

  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second_layout);

    bt1 = (Button) findViewById(R.id.button);
    /*
    * 第二个页面什么时候给第一个页面回传数据
    * 回传第一个页面实际上是一个Intent对象
    * */

    bt1.setOnClickListener(new View.OnClickListener() {
      public void onClick(View view) {
        Intent data = new Intent();//使用空参就行,因为我们不跳转
        data.putExtra("data",value);
        setResult(3,data);
        finish();//销毁本页面,也就是返回上一页面
      }
    });
  }
}

first_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="match_parent"
  android:layout_height="match_parent">

  <Button
    android:id="@+id/button_first"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="第一种启动方式" />

  <Button
    android:id="@+id/button_second"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="第二种启动方式" />

  <TextView
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="把第二个页面回传的数据显示出来" />
</LinearLayout>

second_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="match_parent"
 android:layout_height="match_parent">

 <Button
  android:id="@+id/button"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:text="Button" />
</LinearLayout>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Retrofit2日志拦截器的使用

这篇文章主要介绍了Retrofit2日志拦截器的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android创建外部lib库及自定义View的图文教程

这篇文章主要给大家介绍了关于Android创建外部lib库及自定义View的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序失败的一些事小结

这篇文章主要给大家介绍了关于Android分享微信小程序失败一些事,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序技巧之图片优化

这篇文章主要给大家介绍了关于Android分享微信小程序技巧之图片优化的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android Viewpager实现无限循环轮播图

这篇文章主要为大家详细介绍了Android Viewpager实现无限循环轮播图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中的Bitmap序列化失败的解决方法

这篇文章主要介绍了Android中的Bitmap序列化失败的解决方法,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android自定义通用标题栏CustomTitleBar

这篇文章主要为大家详细介绍了Android自定义通用标题栏CustomTitleBar,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android组合控件自定义标题栏

这篇文章主要为大家详细介绍了Android组合控件自定义标题栏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义复合控件实现通用标题栏

这篇文章主要为大家详细介绍了Android自定义复合控件实现通用标题栏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

ExpandableListView实现简单二级列表

这篇文章主要为大家详细介绍了ExpandableListView实现简单二级列表,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多