http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 2146|回复: 4

[新手开发之旅] Android新手开发之旅-Activity间的跳转和传值(二)

[复制链接]
发表于 2018-12-11 17:19:36 | 显示全部楼层 |阅读模式
本帖最后由 liu 于 2018-12-11 17:18 编辑

Activity间的跳转和传值




上次简单说了显式跳转和隐式跳转,这次来说下带返回值的跳转和不带返回值的跳转
能传递的值有很多种类型:

         QQ截图20181211162153.png



不带返回值(以其中一种传值的类型举例):
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);
    }

}

效果图:

         x.gif
        


注意: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();
                }
            }
        });
    }

}


效果图:

         x.gif














1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2018-12-11 17:40:08 | 显示全部楼层
无回帖,不论坛,这才是人道。
发表于 2018-12-11 17:43:48 | 显示全部楼层
我只是路过打酱油的。
发表于 2018-12-13 14:18:36 | 显示全部楼层
强烈支持楼主ing……
发表于 2018-12-16 22:27:05 | 显示全部楼层
真是难得给力的帖子啊。
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-4-23 14:29

© 2014-2021

快速回复 返回顶部 返回列表