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

时间: 2024-08-02 03:25:08

Android 实现页面跳转的相关文章

应用-Android自定义页面跳转动画

问题描述 Android自定义页面跳转动画 <br> <item name="android:activityOpenEnterAnimation">@anim/move_right_in_activity</item><br> <item name="android:activityOpenExitAnimation">@anim/move_left_out_activity</item>&l

Android Activity中使用Intent实现页面跳转与参数传递的方法_Android

本文实例讲述了Android Activity中使用Intent实现页面跳转与参数传递的方法.分享给大家供大家参考,具体如下: 新建一个FirstAvtivity.java package com.zhuguangwei; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.O

Android应用自动跳转到应用市场详情页面的方法_Android

前言 众所周知在Android应用开发过程中,可能会有需求,比如:推广时跳转到应用市场下载应用,跳转到应用市场给自己的应用打分,跳转到应用市场更新自己的应用. 那如何跳转到应用市场呢? 可能跳转的方法大家都是知道的,方法如下: public static void goToMarket(Context context, String packageName) { Uri uri = Uri.parse("market://details?id=" + packageName); Int

php开发中的页面跳转方法总结_php技巧

页面跳转可能是由于用户单击链接.按钮等触发的,也可能是系统自动产生的.页面自动跳转在WEB开发中经常用到,而且根据需求可以采用不同的跳转方式,比如提示操作信息后延时跳转等, 本文总结了WEB开发中常见的几种页面跳转方法. PHP header()函数跳转 PHP的header()函数非常强大,其中在页面url跳转方面也调用简单,使用header()直接跳转到指定url页面,这时页面跳转是302重定向: $url = "http://www.jb51.net/"; header( &qu

android Fragment切换跳转子页面空指针异常

问题描述 android Fragment切换跳转子页面空指针异常 我有四个fragment,进程序默认加载第一个,我点击了第四个fragment,然后点击它的地址列表!就会报首页轮播图的空指针异常,哪位大神吗有大概思路,给提示下 解决方案 手动抛出一个异常--- 解决方案二: http://www.51zxw.net/study.asp?vip=12576487

界面-Android 从activity跳转进入另一个activity内viewpager的指定页面

问题描述 Android 从activity跳转进入另一个activity内viewpager的指定页面 求助: 现在的需求是, 要从activityA跳转进入另一个activityB内viewpager的第4个界面 (activityB一共有4个fragment组成viewpager) 注:看清需求,不是activityA跳转到activityB,而是要跳转到activityB内部viewpager里面的一个指定页面 解决方案 直接在activityB中设置viewPager.setCurre

华为 动画效果 无效-android 仿微信在application里面设置页面跳转动画的theme 有些机型没有效果

问题描述 android 仿微信在application里面设置页面跳转动画的theme 有些机型没有效果 想做一个仿微信的左右切换动画,根据这篇文章的写法 ,发现,华为p7 系统是4.4.2 上面,根本不起作用,还是手机默认的动画效果,只有在代码里写 override 动画才起作用,求解决方案

android开发-Android Fragment页面如何实现点击跳载Activity?

问题描述 Android Fragment页面如何实现点击跳载Activity? Android Fragment页面如何实现点击跳载到另一个Activity页面? 解决方案 Intent it = new Intent(getActivity(), DestinationActivity.class); getActivity().startActivity(intent);

上下文菜单 传值-Android上下文菜单页面跳转无法传值

问题描述 Android上下文菜单页面跳转无法传值 case 1: int keepAcc_id = Integer.valueOf(((TextView) menuInfo.targetView .findViewById(R.id.keepAcc_id)).getText().toString()); switch (item.getItemId()) { case 0: Intent intent1 = new Intent(); intent1.setClass(this, KeepAc