android 实现页面跳转代码,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
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
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"/>
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"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
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"/>
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"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
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"
/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/rg">
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"/>
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"/>
android:layout_width="match_parent"
android:layout_height="wrap_content">
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="兴趣爱好:"/>
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="唱歌"/>
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="游泳"/>
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="看书"/>
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="6dp"
android:textSize="15dp"
android:text="所在地"/>
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">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"
android:layout_marginTop="10dp"
android:textSize="20dp"
android:id="@+id/btnLogin"/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="显示信息"
android:id="@+id/tvShow"/>
activity_main2.xml
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">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:text="显示结果"
android:id="@+id/tvShow"/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="返回结果"
android:id="@+id/btnBack"/>
总结
以上所述是小编给大家介绍的Android 实现页面跳转,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
android 实现页面跳转代码,Android 实现页面跳转相关推荐
- android 仿ios动画效果代码,Android仿IOS上拉下拉弹性效果的实例代码
用过iphone的朋友相信都体验过页面上拉下拉有一个弹性的效果,使用起来用户体验很好:Android并没有给我们封装这样一个效果,我们来看下在Android里如何实现这个效果.先看效果,感觉有些时候还 ...
- android博学谷我的代码,Android项目实战系列—基于博学谷(四)我的模块(下)...
由于这个模块内容较多,篇幅较长,请耐心阅读. "我"的模块分为四个部分 [ ] [我的界面]() [ ] [设置界面]() [x] [修改密码界面]() [x] [设置密保和找回密 ...
- android建立电话拨号器代码,Android编写电话拨号器
一.实现步骤 1.需要创建拨号意图 Intent intent = new Intent(); intent.setAction(Intent.ACTION_CALL); intent.setData ...
- php跳转代码循环,PHP页面跳转代码几种方法
在php中要实现跳转有很多方法,最常规的跳转方法就是使用header函数来操作了,当然也可以在php中输入js跳转形式,下面我来给大家介绍一下. PHP 跳转 代码如下 复制代码 header(&q ...
- android 微信设置圆角边框代码,Android编程实现圆角边框的方法
本文实例讲述了Android编程实现圆角边框的方法.分享给大家供大家参考,具体如下: 设置边框圆角可以在drawable-mdpi目录里定义一个xml: android:topRightRadius= ...
- android 微信摇一摇代码,Android实现微信摇一摇功能
本文实例为大家分享了Android实现微信摇一摇功能的具体代码,供大家参考,具体内容如下 1.初始化界面 设置摇一摇界面的背景图片和摇动时的上下两半张图片 xmlns:tools="http ...
- android和js交互的代码,Android与JS代码交互
现如今,很多Android APP都要与JS进行交互,而WebView就是Android与JS交互的桥梁,日常中最常见Android和JS交互有:APP注册协议,APP里的文章,APP里的活动页,这都 ...
- android填空题界面的代码,Android 使用代码实现一个选词(拖拽)填空题
封面 1.写在前面 在上一篇文章<Android 使用代码实现一个填空题>中,我们学习了如何实现一个填空题,今天继续接着上一篇文章的节奏,学习一下如何实现一个选词填空题,由于本文中用到了一 ...
- android手机通讯录备份还原代码,android手机通讯录备份还原代码
最近想写段android程序玩玩. 开发环境 eclipse ,android2.2 开发环境搭建 1.先安装jdk 2.下载安装eclipse 3.下载安装android sdk 4.安装eclip ...
- android studio连接服务端代码,Android studio - 无法连接到LDAP服务器(示例代码)
我想在android studio中使用LDAP,我在最新版本中使用UnboundID LDAP SDK for Java. 我使用命令: LDAPConnection ldap = new LDAP ...
最新文章
- echart折线图,柱状图,饼图设置颜色
- EPSON机器人_SPEL+语言
- SpringBoot集成Redis实现排行榜
- [THINKING IN JAVA]访问权限控制
- java timer 序列化_编程达人 应用层timer_如何序列化timer
- pytorch torchvision.datasets.ImageFolder
- 京东物流首架全货机首航 久未露面的刘强东还发声推广
- 笔记:UITextView内容垂直居中方法
- Hyperledger Fabric MSP Identity Validity Rules——MSP身份验证规则
- 打开unity卡在loading白屏界面
- 豪迈HOMAG变频器维修E107544豪迈变频器维修09F5C3B
- 特征筛选【IV和WOE】
- html5 黑色圆圈,html5使用canvas画空心圆与实心圆_html5教程技巧
- python语言求球的体积,改进蒙特卡罗程序求高维球体体积。(Python)
- 强制使用ie浏览器使用最高版本
- 计算机是如何进行计算的?(二)
- Python 为何能坐稳 AI 时代头牌语言
- 管理学十二(流程与制度的重要性)
- 中北大学算法分析与设计实验报告一(BF算法)
- 做完这套面试题,你才敢说懂Excel