本帖最后由 liu 于 2018-12-11 17:18 编辑
Activity间的跳转和传值
上次简单说了显式跳转和隐式跳转,这次来说下带返回值的跳转和不带返回值的跳转
能传递的值有很多种类型:
不带返回值(以其中一种传值的类型举例):
activity_main.xml:
[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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<EditText
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:hint="请输入要传的值" />
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="开始传值" />
</LinearLayout>
MainActivity:
[Java] 纯文本查看 复制代码 package com.company.helloworld.firstapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private Button button;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.btn);
editText = (EditText) findViewById(R.id.et);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
//第一个参数是自定义的名字,用于目标Activity接收所传递的值时使用 第二个参数是所传的值
intent.putExtra("content", editText.getText().toString());
startActivity(intent);
}
});
}
}
activity_second.xml:
[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"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="接收到的值:"
android:textColor="@android:color/black" />
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:layout_marginLeft="15dp" />
</LinearLayout>
SecondActivity:
[Java] 纯文本查看 复制代码 package com.company.helloworld.firstapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
TextView tv = (TextView) findViewById(R.id.tv);
//获取所传的值
String content = getIntent().getStringExtra("content");
tv.setText(content);
}
}
效果图:
注意:1、使用getIntent()接收值的时候所调用方法的返回值类型要和传值时所传的值的类型一致
2、传值和取值时的自定义的名字要保持一致
带返回值:
需要重写MainActivity中的onActivityResult方法,在里面接收返回来的值
例如:
activity_main.xml:
[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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="跳转" />
<LinearLayout
android:id="@+id/layout"
android:visibility="gone"
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:textColor="@android:color/black" />
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:textColor="@android:color/black" />
</LinearLayout>
</LinearLayout>
MainActivity:
[Java] 纯文本查看 复制代码 package com.company.helloworld.firstapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button button;
private TextView textView;
private LinearLayout layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.btn);
textView = (TextView) findViewById(R.id.tv);
layout = (LinearLayout) findViewById(R.id.layout);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivityForResult(intent, 1);
}
});
}
/**
* @param requestCode 请求码,即调用startActivityForResult()传递过去的值
* @param resultCode 结果码,结果码用于标识返回数据来自哪个新Activity
* @param data 从中取数据
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (resultCode == 1) {
layout.setVisibility(View.VISIBLE);
String content = data.getStringExtra("content");
textView.setText(content);
}
}
}
}
activity_second.xml:
[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"
android:gravity="center"
android:orientation="vertical">
<EditText
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:hint="请输入要返回的值"
android:textColor="@android:color/black" />
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="返回" />
</LinearLayout>
SecondActivity:
[Java] 纯文本查看 复制代码 package com.company.helloworld.firstapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class SecondActivity extends AppCompatActivity {
private EditText editText;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
button = (Button) findViewById(R.id.btn);
editText= (EditText) findViewById(R.id.et);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//判断是否为空
if (!TextUtils.isEmpty(editText.getText().toString())) {
Intent intent = new Intent();
intent.putExtra("content", editText.getText().toString());
setResult(1, intent);
finish();
} else {
Toast.makeText(SecondActivity.this, "请输入内容", Toast.LENGTH_SHORT).show();
}
}
});
}
}
效果图:
|