Intent页面跳转(有、无返回值两种实例)
2018-02-02 10:48
423 查看
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.prace3.MainActivity"> <Button android:id="@+id/btn1" android:text="第一种无返回" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btn2" android:text="第二种有返回" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/text" android:text="把第二页面返回数据展示" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
activity_second.xml
<?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/btn" android:text="第二页的按钮" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
AndroidManifest.xml 配置文件
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.prace3"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- 添加第二个页面启动 --> <activity android:name=".SecondActivity" android:label="@string/app_name" android:theme="@style/AppTheme"> </activity> </application> </manifest>
MainActivity.java
package com.example.prace3; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { private Button btn1; private Button btn2; private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化控件 btn1 = findViewById(R.id.btn1); btn2 = findViewById(R.id.btn2); tv = findViewById(R.id.text); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this,SecondActivity.class); startActivity(intent); } }); btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this,SecondActivity.class); /* 第一个参数Intent对象 第二个参数请求的标识 */ startActivityForResult(intent,1); } }); } /* 通过startActivityForResult跳转,接收返回数据 requestCode 请求的标识 resultCode 第二个页面返回标识 data 第二页面回传数据 */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1 && resultCode == 2){ String content = data.getStringExtra("data"); tv.setText(content); } } }
SecondActivity.java
package com.example.prace3; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; /** * Created by gqr on 2018/2/2. */ public class SecondActivity extends Activity { private Button btn; private String content = "hdbvcjhdjvnd"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); btn = findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent data = new Intent(); data.putExtra("data",content); setResult(2,data); finish();//关闭页面 } }); } }至此,就可以实现点击第一页面第一个按钮实现无返回值跳转;点击第二个按钮跳转第二页再点击第二页按钮,第二页关闭,在第一页中展示返回数据(字符串)。
相关文章推荐
- 安卓——Intent(实现页面跳转的两种方法)
- 前台页面跳转的例子(两种实例)
- Android Intent实现页面跳转的两种方法
- android自学 intent 页面跳转的两种方法
- IOS Xib控件拖拽与页面跳转实例
- Java后台和Jsp跳转页面的两种方法以及它们的区别
- 微信小程序--跳转页面的两种方法详解
- Android成长日记-使用Intent实现页面跳转
- a标签根据js返回值判断页面是否跳转
- android开发基础:Intent与Bundle的使用,实现页面跳转及信息传递
- Struts2中action重定向跳转到jsp页面两种配置方式
- Android使用Intent实现页面跳转
- 使用JS实现页面内跳转的两种方式
- 11. jsp与servlet之间页面跳转及参数传递实例
- JSP页面跳转的两种方式----重定向和转发
- android:跳转,Intent,有无返回值
- Android学习-Intent实现页面的跳转
- 页面跳转的的两种方式的异同
- Android学习(4)之页面间的传值——Intent意图对象的两种方式
- JQuery -AJAX- html中提交表单并实现不跳转页面处理返回值