当前位置: 移动技术网 > 移动技术>移动开发>Android > Android 实现页面跳转

Android 实现页面跳转

2019年07月24日  | 移动技术网移动技术  | 我要评论
android使用intent来实现页面跳转,intent通过startactivity(intent intent)或startactivityforresult(int

android使用intent来实现页面跳转,intent通过startactivity(intent intent)或startactivityforresult(intent intent,int resquestcode)方法来启动activity,在新建intent对象时来指定从a页面跳到b页面,

比如:

intent i = new intent(a.this,b.class);这就表示从a页面跳到b页面, 

intent对象通过调用putextra方法来传递页面跳转时所需要传递的信息

比如:

putextra(“给需要传递的信息命名”,需要传递的信息的内容)

intent通过调用getstringextra方法来接受传递过来的信息

getstringextra(“传递过来的信息的名字”);

下面的代码将实现用户输入完信息之后点击登入按钮,页面将跳转到另一页面显示个人信息,然后在这个页面有一个返回按钮,点击返回按钮,页面将返回登入页面再次显示个人信息。

mainactivity.java

package com.example.hsy.register;
import android.content.intent;
import android.support.annotation.idres;
import android.support.v7.app.appcompatactivity;
import android.os.bundle;
import android.view.view;
import android.widget.adapterview;
import android.widget.button;
import android.widget.checkbox;
import android.widget.compoundbutton;
import android.widget.edittext;
import android.widget.radiobutton;
import android.widget.radiogroup;
import android.widget.spinner;
import android.widget.textview;
public class mainactivity extends appcompatactivity {
  edittext etname,etpwd;
  button btnlogin;
  textview tvshow;
  radiogroup rg;
  radiobutton rbmale,rbfelmale;
  checkbox checkbox1,checkbox2,checkbox3;
  spinner spcity;
  string sex="";
  string hobby1="",hobby2="",hobby3="";
  string result="";
  string city="";
  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.test);
    init();
    rglistener();
    btnloginlistener();
    box1listener();
    box2listener();
    box3listener();
    splistener();
  }
  @override
  protected void onactivityresult(int requestcode, int resultcode, intent data) {
    super.onactivityresult(requestcode, resultcode, data);
    tvshow.settext("返回结果是:"+"\n"+data.getstringextra("result1").tostring()+
        "\n");
  }
  private void splistener() {
    spcity.setonitemselectedlistener(new adapterview.onitemselectedlistener() {
      @override
      public void onitemselected(adapterview<?> parent, view view, int position, long id) {
        city=(string) spcity.getselecteditem();
      }
      @override
      public void onnothingselected(adapterview<?> parent) {
      }
    });
  }
  private void box3listener() {
    checkbox3.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() {
      @override
      public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) {
        if(ischecked){
          hobby3="看书";
        } else {
          hobby3="";
        }
      }
    });
  }
  private void box2listener() {
    checkbox2.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() {
      @override
      public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) {
        if(ischecked){
          hobby2="游泳";
        } else {
          hobby2="";
        }
      }
    });
  }
  private void box1listener() {
    checkbox1.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() {
      @override
      public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) {
        if(ischecked){
          hobby1="唱歌";
        } else {
          hobby1="";
        }
      }
    });
  }
  private void btnloginlistener() {
    btnlogin.setonclicklistener(new view.onclicklistener(){
      @override
      public void onclick(view v){
        result="用户名是:"+etname.gettext().tostring()+"\n"+"密码是:"+
            etpwd.gettext().tostring()+"\n"+"性别是:"+sex+"\n"+"爱好是:"+hobby1+" "
            +hobby2+" "+hobby3+" "+
        "\n"+"所在城市:"+city;
        //tvshow.settext(result);
        intent i = new intent(mainactivity.this,main2activity.class);
        i.putextra("data1",result);
        startactivityforresult(i,0);
      }
    });
  }
  private void rglistener() {
    rg.setoncheckedchangelistener(new radiogroup.oncheckedchangelistener(){
      @override
      public void oncheckedchanged(radiogroup group, @idres int checkedid){
        if(checkedid== r.id.rbmale)
          sex="男生";
        else
          sex="女生";
      }
    });
  }
  private void init() {
    etname=(edittext) findviewbyid(r.id.etname);
    etpwd=(edittext) findviewbyid(r.id.etpwd);
    btnlogin=(button) findviewbyid(r.id.btnlogin);
    tvshow=(textview) findviewbyid(r.id.tvshow);
    rg=(radiogroup) findviewbyid(r.id.rg);
    rbmale=(radiobutton) findviewbyid(r.id.rbmale);
    rbfelmale=(radiobutton) findviewbyid(r.id.rbfemale);
    checkbox1=(checkbox) findviewbyid(r.id.checkbox1);
    checkbox2=(checkbox) findviewbyid(r.id.checkbox2);
    checkbox3=(checkbox) findviewbyid(r.id.checkbox3);
    spcity=(spinner) findviewbyid(r.id.spcity);
  }
}

mainactivity2.java

package com.example.hsy.register;
import android.content.intent;
import android.support.v7.app.appcompatactivity;
import android.os.bundle;
import android.view.view;
import android.widget.textview;
public class main2activity extends appcompatactivity {
  textview tvshow;
  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main2);
    tvshow=(textview)findviewbyid(r.id.tvshow);
    intent intent = getintent();
    tvshow.settext(intent.getstringextra("data1").tostring());
    findviewbyid(r.id.btnback).setonclicklistener(new view.onclicklistener() {
      @override
      public void onclick(view v) {
        intent intent1=new intent(main2activity.this,mainactivity.class);
        intent1.putextra("result1",tvshow.gettext().tostring());
        setresult(1,intent1);
        finish();
      }
    });
  }
}

test.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">
  <linearlayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <textview
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="用户名:"
      android:layout_marginleft="10dp"
      android:layout_margintop="10dp"
      android:textsize="20dp"
      android:textcolor="@color/colorprimarydark"/>
    <edittext
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:hint="输入2-10个字符"
      android:layout_marginleft="10dp"
      android:layout_margintop="10dp"
      android:layout_weight="1"
      android:id="@+id/etname"/>
  </linearlayout>
  <linearlayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <textview
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="密  码:"
      android:layout_marginleft="10dp"
      android:layout_margintop="10dp"
      android:textsize="20dp"
      android:textcolor="@color/colorprimarydark"/>
    <edittext
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:hint="输入6-10个字符"
      android:layout_marginleft="10dp"
      android:layout_margintop="10dp"
      android:layout_weight="1"
      android:id="@+id/etpwd"/>
  </linearlayout>
  <linearlayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <textview
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="选择性别:"
      android:layout_margintop="10dp"
      android:layout_marginleft="10dp"
      android:textsize="20dp"
      android:textcolor="@color/colorprimary"
      />
    <radiogroup
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      android:id="@+id/rg">
      <radiobutton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginleft="15dp"
        android:layout_margintop="10dp"
        android:textcolor="@color/coloraccent"
        android:textsize="10dp"
        android:text="男"
        android:id="@+id/rbmale"/>
      <radiobutton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginleft="15dp"
        android:layout_margintop="10dp"
        android:textcolor="@color/coloraccent"
        android:textsize="10dp"
        android:text="女"
        android:id="@+id/rbfemale"/>
    </radiogroup>
  </linearlayout>
  <linearlayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <textview
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginleft="10dp"
      android:layout_margintop="10dp"
      android:textsize="20dp"
      android:textcolor="@color/coloraccent"
      android:text="兴趣爱好:"/>
    <checkbox
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginleft="10dp"
      android:layout_margintop="10dp"
      android:textsize="15dp"
      android:textcolor="@color/coloraccent"
      android:id="@+id/checkbox1"
      android:text="唱歌"/>
    <checkbox
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginleft="10dp"
      android:layout_margintop="10dp"
      android:textsize="15dp"
      android:textcolor="@color/coloraccent"
      android:id="@+id/checkbox2"
      android:text="游泳"/>
    <checkbox
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginleft="10dp"
      android:layout_margintop="10dp"
      android:textsize="15dp"
      android:textcolor="@color/coloraccent"
      android:id="@+id/checkbox3"
      android:text="看书"/>
  </linearlayout>
  <linearlayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <textview
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginleft="5dp"
      android:layout_margintop="6dp"
      android:textsize="15dp"
      android:text="所在地"/>
    <spinner
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_margintop="6dp"
      android:layout_marginleft="10dp"
      android:entries="@array/citys"
      android:id="@+id/spcity">
    </spinner>
  </linearlayout>
  <button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="登录"
    android:layout_margintop="10dp"
    android:textsize="20dp"
    android:id="@+id/btnlogin"/>
  <textview
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margintop="20dp"
    android:text="显示信息"
    android:id="@+id/tvshow"/>
</linearlayout>

activity_main2.xml

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context="com.example.hsy.register.main2activity">
  <textview
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margintop="10dp"
    android:layout_marginleft="10dp"
    android:text="显示结果"
    android:id="@+id/tvshow"/>
  <button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginleft="10dp"
    android:layout_margintop="10dp"
    android:text="返回结果"
    android:id="@+id/btnback"/>
</linearlayout>

总结

以上所述是小编给大家介绍的android 实现页面跳转,希望对大家有所帮助

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网