全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

Android 实现页面跳转

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 实现页面跳转,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!


# android  # 实现页面跳转  # Android编程中Intent实现页面跳转功能详解  # Android实现页面跳转的全过程记录  # Android实现注册页面(携带数据包跳转)  # Android统一处理登录后携带数据跳转到目标页面的方式  # 跳转  # 方法来  # 登入  # 个人信息  # 跳到  # 小编  # 在这个  # 在此  # 这就  # 给大家  # 所需要  # 来实现  # 所述  # 给我留言  # 感谢大家  # 兴趣爱好  # 跳转到  # 有一个  # 疑问请  # 有任何 


相关文章: 成都网站制作报价公司,成都工业用气开户费用?  建站之星与建站宝盒如何选择最佳方案?  零服务器AI建站解决方案:快速部署与云端平台低成本实践  小自动建站系统:AI智能生成+拖拽模板,多端适配一键搭建  建站之星后台管理:高效配置与模板优化提升用户体验  建站之星安装失败:服务器环境不兼容?  上海制作企业网站有哪些,上海有哪些网站可以让企业免费发布招聘信息?  如何用已有域名快速搭建网站?  大连企业网站制作公司,大连2025企业社保缴费网上缴费流程?  如何通过虚拟主机空间快速建站?  杭州银行网站设计制作流程,杭州银行怎么开通认证方式?  香港服务器网站搭建教程-电商部署、配置优化与安全稳定指南  c# F# 的 MailboxProcessor 和 C# 的 Actor 模型  建站主机空间推荐 高性价比配置与快速部署方案解析  网站制作员失业,怎样查看自己网站的注册者?  如何配置支付宝与微信支付功能?  网站网页制作专业公司,怎样制作自己的网页?  我的世界制作壁纸网站下载,手机怎么换我的世界壁纸?  深圳网站制作培训,深圳哪些招聘网站比较好?  建站主机SSH密钥生成步骤及常见问题解答?  相亲简历制作网站推荐大全,新相亲大会主持人小萍萍资料?  网站图片在线制作软件,怎么在图片上做链接?  如何快速搭建FTP站点实现文件共享?  小程序网站制作需要准备什么资料,如何制作小程序?  建站VPS选购需注意哪些关键参数?  如何快速上传自定义模板至建站之星?  创业网站制作流程,创业网站可靠吗?  动图在线制作网站有哪些,滑动动图图集怎么做?  油猴 教程,油猴搜脚本为什么会网页无法显示?  怎么用手机制作网站链接,dw怎么把手机适应页面变成网页?  浙江网站制作公司有哪些,浙江栢塑信息技术有限公司定制网站做的怎么样?  如何在IIS7中新建站点?详细步骤解析  制作网站软件推荐手机版,如何制作属于自己的手机网站app应用?  如何通过IIS搭建网站并配置访问权限?  建站之星后台密码遗忘?如何快速找回?  大同网页,大同瑞慈医院官网?  公司门户网站制作公司有哪些,怎样使用wordpress制作一个企业网站?  在线制作视频网站免费,都有哪些好的动漫网站?  中山网站制作网页,中山新生登记系统登记流程?  深圳防火门网站制作公司,深圳中天明防火门怎么编码?  建站之星3.0如何解决常见操作问题?  网站制作的方法有哪些,如何将自己制作的网站发布到网上?  如何将凡科建站内容保存为本地文件?  广平建站公司哪家专业可靠?如何选择?  如何通过虚拟机搭建网站?详细步骤解析  制作宣传网站的软件,小红书可以宣传网站吗?  如何配置IIS站点权限与局域网访问?  如何解决ASP生成WAP建站中文乱码问题?  建站之星伪静态规则如何正确配置?  SAX解析器是什么,它与DOM在处理大型XML文件时有何不同? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。