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"?>                                                                                          

activity_main2.xml

<?xml version="1.0" encoding="utf-8"?>    

总结

以上所述是小编给大家介绍的Android 实现页面跳转,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

更多相关文章

  1. 安全新手入坑——HTML标签
  2. Nginx系列教程(四)| 一文带你读懂Nginx的动静分离
  3. 【Android(安卓)开发教程】使用Intent调用内置应用程序
  4. 6.9 Android(安卓)支付宝 开发者平台申请和接口调用
  5. android 根据apk文件获取包名等信息 已经判断 指定 这个apk 是否
  6. android 自定义Dialog页面加载中效果的进度条 Dialog 边角 Dialo
  7. android PhoneGap 自定义插件 发现信息 转
  8. Android(安卓)button按钮点击过快造成多次事件
  9. Android(安卓)Gallery通过按钮控制选择

随机推荐

  1. 凛冬至,外包咋了,努力照样250!
  2. 抢票加速靠谱?还是黄牛更靠谱?
  3. 国内网游版号重开审批;腾讯、网易等游戏股
  4. 漫谈发版哪些事,好课程推荐
  5. 租房减税30,房东加租300!
  6. 社交软件撬TX墙角,到底还有没有汤喝
  7. 使用表格写课程表代码
  8. 开学第二天作业-H5 课程表与注册表单
  9. 常用注册表单功能
  10. 开学第一天作业