Android Intent实现页面跳转的两种方法

时间:2021-05-21

本文实例为大家分享了Intent实现页面跳转的两种的方法,供大家参考,具体内容如下

下图中两个不同的方法就是两种页面之间跳转的情况

1).跳转不返回数据

2).跳转返回数据

实例:

第一种启动方式(跳转不返回数据)

第二种启动方式(跳转返回数据)

先看第一种:

点击第一种启动方式按钮会出现右边的图,然后再点击Button按钮返回左边的界面,TextView中的内容没变。

再看第二种启动方式

不同的是,点击Button按钮返回左边的界面,TextView中的内容变成了你好。

下面是所有代码

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.lenovo.intent"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> </activity> <activity android:name="com.example.lenovo.intent.firstactivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.example.lenovo.intent.Secondactivity"> </activity> </application></manifest>

factivity

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/bt1__first" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="第一种启动方式" /> <Button android:id="@+id/bt2__second" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="第二种启动方式" /> <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="吧第二个页面回传的数据显示出来" /></LinearLayout>

sactivity

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Button" /></LinearLayout>

firstactivity.java

package com.example.lenovo.intent; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; /** * Created by lenovo on 2018/2/27. */ public class firstactivity extends Activity { private Button bt1; private Button bt2; private TextView tv; @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.factivity); bt1=(Button) findViewById(R.id.bt1__first); bt2=(Button)findViewById(R.id.bt2__second); tv=(TextView) findViewById(R.id.textView1); //给bt1添加点击事件 bt1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(firstactivity.this,Secondactivity.class); startActivity(intent); } }); //给bt2添加点击事件 bt2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(firstactivity.this,Secondactivity.class); startActivityForResult(intent,1); } }); } @Override protected void onActivityResult(int requestCode,int resultCode ,Intent data){ super.onActivityResult(requestCode,resultCode,data); if(requestCode==1&&resultCode==2){//当请求码是1&&返回码是2进行下面操作 String content=data.getStringExtra("data"); tv.setText(content); } } }

Secondactivity.java

package com.example.lenovo.intent; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; /** * Created by lenovo on 2018/2/27. */ public class Secondactivity extends Activity { private Button bt; String content="你好";//想返回的内容 @Override protected void onCreate( Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sactivity); bt=(Button) findViewById(R.id.button); bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent data = new Intent(); //name相当于一个key,content是返回的内容 data.putExtra("data",content); //resultCode是返回码,用来确定是哪个页面传来的数据,这里设置返回码是2 //这个页面传来数据,要用到下面这个方法setResult(int resultCode,Intent data) setResult(2,data); //结束当前页面 finish(); } }); } }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章