您的位置:首页 > 移动开发 > Android开发

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();//关闭页面
}
});
}
}
至此,就可以实现点击第一页面第一个按钮实现无返回值跳转;点击第二个按钮跳转第二页再点击第二页按钮,第二页关闭,在第一页中展示返回数据(字符串)。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息