当前位置: 移动技术网 > IT编程>移动开发>Android > Android使用Intent实现页面跳转

Android使用Intent实现页面跳转

2019年07月24日  | 移动技术网IT编程  | 我要评论

心晴qaq,叱咤风云 唐僧志,完美江河网站

什么是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>

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网